Nginx domain forwarding usage scenario code example

Nginx domain forwarding usage scenario code example

Scenario 1: Due to server restrictions, only one port is open to the outside world, but a different external network environment needs to be requested, so nginx is used on the transit server to forward the data.

accomplish:

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/; } }

Special Note:

Note: If the file upload forwarding is involved here, add client_max_body_size 100m under server_name;

The domain name forwarding address, proxy_pass must be followed by "/", otherwise it will cause abnormal forwarding

When configuring proxy_pass proxy forwarding in nginx, if you add / to the URL after proxy_pass, it means the absolute root path;
If there is no /, it means a relative path, and the matching path part is also proxied.

Assume that the following four situations are accessed using http://192.168.1.1/proxy/aerchi.html respectively.

The first one:

location /proxy/ {
proxy_pass http://127.0.0.1/;
}

Proxy to URL: http://127.0.0.1/aerchi.html

The second type (compared to the first type, with one less / at the end)

location /proxy/ {
proxy_pass http://127.0.0.1;
}

Proxy to URL: http://127.0.0.1/proxy/aerchi.html

The third type:

location /proxy/ {
proxy_pass http://127.0.0.1/aaa/;
}

Proxy to URL: http://127.0.0.1/aaa/aerchi.html

The fourth type (compared to the third type, with one less / at the end)

location /proxy/ {
proxy_pass http://127.0.0.1/aaa;
}

Proxy to URL: http://127.0.0.1/aaaaerchi.html

Scenario 2: Due to business needs, a set of front-end code needs to be mapped to two back-end addresses, so it is necessary to distinguish and forward on the interface, and the distinguishing mark needs to be removed during forwarding

accomplish:

server {
  listen 0.0.0.0:8204;
  server_name localhost;
  #Static page directory root E:\xxxxxxx;
  #Default homepage 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/(.*) {#Distinguish dynamic pages based on whether the interface contains /wx/# and hand them over to tomcat for processing if (!-e $request_filename) {
        proxy_pass http://127.0.0.1:8091/$1;#When going to the backend, you need to remove /wx}
  }
  location / {
    # User browser cache settings 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, handed over to tomcat for processing 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;
  }
}

Special Note:

1. proxy_set_header Host $http_host;

The request headers are not changed.

2. proxy_set_header Host host; if the client request header does not carry this header, the request passed to the backend server will not contain this header. In this case, host is used; if the client request header does not carry this header, then the request passed to the backend server does not contain this header either. In this case, host is used; if the client request header does not carry this header, then the request passed to the backend server does not contain this header either. In this case, the host variable is used. Its 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 passed along with the port of the backend server:

4. If the value of a request header is empty, then the request header will not be sent to the backend server:

proxy_set_header Accept-Encoding "";

5. The user's real IP address is forwarded to the backend server

proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Real-IP $remote_addr;

Scenario 3: The previous code is implemented with Vue. Vue does not have a specific page and also accesses resources through /xx/xx. At this time, it needs to be distinguished from the background interface

accomplish:

They can be distinguished by special characters like "#". 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: Due to server restrictions, database access requires nginx forwarding

accomplish:

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; #Local proxy port proxy_connect_timeout 10s;
    proxy_timeout 300s;#Set the timeout between the client and the proxy service. If there is no operation within 5 minutes, it will be automatically disconnected.
    proxy_pass cloudsocket;
  }

}

Special attention:

stream is at the same level as http, so don't put it in http

Scenario 5: When accessing a domain name, you need to redirect to another address

accomplish:

server {
  listen 7000;
  server_name localhost;
  client_max_body_size 100m;

  location = / {
    rewrite ^(.*) https://www.baidu.com permanent;
  }
}

Special attention:

last After this rule is matched, continue to match the new location URI rule
break This rule is terminated after matching, and no more rules are matched
redirect returns a 302 temporary redirect
permanent returns a 301 permanent redirect

The above is the full content of this article. I hope it will be helpful for everyone’s study. I also hope that everyone will support 123WORDPRESS.COM.

You may also be interested in:
  • How to set Nginx to forward the domain name to the specified port
  • Implementation of Nginx domain name forwarding
  • Using nginx forward proxy to implement intranet domain name forwarding process analysis
  • Nginx reverse proxy is used for intranet domain name forwarding
  • Implementation of HTTP and HTTPS services with Nginx reverse proxy for multiple domain names
  • Use nginx + secondary domain name + https support
  • Nginx defines domain name access method
  • nginx+tomcat example of accessing the project through the domain name

<<:  The difference between JS pre-parsing and variable promotion in web interview

>>:  Problems and solutions encountered when connecting node to mysql database

Recommend

mysql zip file installation tutorial

This article shares the specific method of instal...

An audio-visual Linux distribution that appeals to audiophiles

I recently stumbled upon the Audiovisual Linux Pr...

Vue implements the magnifying glass effect of tab switching

This article example shares the specific code of ...

Complete steps for Nginx to configure anti-hotlinking

need: Usually, sites want to prevent videos and p...

Implementation of HTML to PDF screenshot saving function

Using Technology itext.jar: Convert byte file inp...

The best 9 foreign free picture material websites

It is difficult to find good image material websi...

In-depth analysis of nginx+php-fpm service HTTP status code 502

One of our web projects has seen an increase in t...

Using JS to implement a simple calculator

Use JS to complete a simple calculator for your r...

Introduction to deploying selenium crawler program under Linux system

Table of contents Preface 1. What is selenium? 2....