Code example of Nginx domain name forwarding usage scenario
- 2021-09-05 01:21:21
- OfStack
Scenario 1: Because of the server limitation, only one port is opened to the outside world, but different external network environment needs to be requested, so nginx is used for forwarding once on the transit server
Achieve:
server {
listen 8051;
server_name localhost;
location /license/ {
proxy_pass http://xxx.xxx.xxx.xxx:8058/;
}
location / {
proxy_pass http://xxx.xxx.xxx.xxx:8051/; } }
Pay special attention to:
Knock on the blackboard: If the forwarding of file upload is involved here, add client_max_body_size 100m under server_name;
The address of domain name forwarding, proxy_pass must be followed by "/", otherwise it will cause abnormal forwarding
When proxy_pass proxy forwarding is configured in nginx, if url after proxy_pass is added with/, it indicates absolute root path;
If there is no/, it means the relative path, and the matching path part is also given to the agent.
Assume that the following four situations are accessed by http://192.168.1.1/proxy/aerchi. html.
Type 1:
location/proxy/{
proxy_pass http://127.0.0.1/;
}
Agent to URL: http://127.0. 0.1/aerchi.html
Type 2 (1/less than type 1)
location/proxy/{
proxy_pass http://127.0.0.1;
}
Agent to URL: http://127.0. 0.1/proxy/aerchi. html
Type 3:
location/proxy/{
proxy_pass http://127.0. 0.1/aaa/;
}
Agent to URL: http://127.0. 0.1/aaa/aerchi. html
The fourth kind (compared with the third kind, the last one is less than/)
location/proxy/{
proxy_pass http://127.0. 0.1/aaa;
}
Agent to URL: http://127.0.0.1/aaaaerchi. html
Scenario 2: Due to business needs, one set of front-end codes needs to be mapped to two back-end addresses, so it is necessary to differentiate and forward on the interface, and at the same time, it is necessary to remove the distinction mark when forwarding
Achieve:
server {
listen 0.0.0.0:8204;
server_name localhost;
# Static page directory
root E:\xxxxxxx;
# Default home page
index /index.html;
proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
#proxy_cookie_path /* /*;
client_max_body_size 100m;
location ~*/wx/(.*) {# Whether the root-out interface contains /wx/ To distinguish
# Dynamic page , Hand over tomcat Deal with
if ( !-e $request_filename) {
proxy_pass http://127.0.0.1:8091/$1;# When you go to the background, you need to put /wx Remove
}
}
location / {
# Cache settings on the user's browser side
location ~* \.(css|js|jpg|jpeg|gif|png|swf|htm|html|json|xml|svg|woff|ttf|eot|map|ico)$ {
expires -1;
if (-f $request_filename) {
break;
}
}
# Dynamic page , Hand over tomcat Deal with
if ( !-e $request_filename) {
proxy_pass http://127.0.0.1:8092;
#proxy_cookie_path /* /*;
}
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
Pay special attention to:
1. proxy_set_header Host $http_host;
The request header is not changed.
2. proxy_set_header Host host; If the client request header does not carry this header, then the request delivered to the back-end server does not contain this header. In this case, use host; If the client request header does not carry this header, then the request delivered to the back-end server does not contain this header. In this case, use host; If the client request header does not carry this header, then the request delivered to the back-end server does not contain this header. In this case, use the host variable whose value is the value of the "Host" field when the request contains the "Host" request header and the primary domain name of the virtual host when the request does not carry the "Host" request header;
3. proxy_set_header Host host: host: host: proxy_port;
The server name can be transferred from Port 1 of the back-end server:
4. If the value of a request header is null, the request header will not be transmitted to the back-end server:
proxy_set_header Accept-Encoding "";
5. Forward the user's real ip address to the back-end server
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_E202EN;
proxy_set_header X-Real-IP $remote_addr;
Scenario 3: The preceding code is implemented with vue, and vue has no specific page, and it also accesses resources through/xx/xx, which needs to be distinguished from the background interface
Achieve:
It can be distinguished by special characters like "#", and requests with # are static resources by default
location ~* \.(#|css|js|jpg|jpeg|gif|png|swf|htm|html|json|xml|svg|woff|ttf|eot|map|ico)$ {
expires -1;
if (-f $request_filename) {
break;
}
}
Scenario 4: Database access requires nginx for forwarding due to server constraints
Achieve:
stream {
upstream cloudsocket {
hash $remote_addr consistent;
server Database Actual ip:3306 weight=5 max_fails=3 fail_timeout=30s;
}
server {
listen 127.0.0.1:8058; # Native agent port
proxy_connect_timeout 10s;
proxy_timeout 300s;# Set the timeout between the client and the proxy service, if 5 Failure to operate within minutes will automatically disconnect.
proxy_pass cloudsocket;
}
}
Pay special attention to:
stream is the same class as http, so don't put it in http
Scenario 5: Redirect to another address when accessing a domain name
Achieve:
server {
listen 7000;
server_name localhost;
client_max_body_size 100m;
location = / {
rewrite ^(.*) https://www.baidu.com permanent;
}
}
Pay special attention to:
last After matching this rule, continue to match the new location URI rule downward
break This rule is terminated after matching, and no rules are matched
redirect Returns 302 Temporary Redirection
permanent returns 301 permanent redirection