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

Introduction to the use of CSS3 filter attribute

1. Introduction When writing animation effects fo...

Common front-end JavaScript method encapsulation

Table of contents 1. Enter a value and return its...

A brief discussion on the principle of shallow entry and deep exit of MySQL

Table of contents 1. Overview of the page 2. Infi...

Implementation process of nginx high availability cluster

This article mainly introduces the implementation...

How to use VirtualBox to simulate a Linux cluster

1. Set up HOST on the host Macbook The previous d...

Vue uses vue meta info to set the title and meta information of each page

title: vue uses vue-meta-info to set the title an...

Brief analysis of mysql scheduled backup tasks

Introduction In a production environment, in orde...

Simple steps to create a MySQL container with Docker

Preface We have already installed Docker and have...

Detailed explanation of the steps to create a web server with node.js

Preface It is very simple to create a server in n...

Getting Started Tutorial on Animating SVG Path Strokes Using CSS3

Without relying on JavaScript, pure CSS is used t...

MySQL 8.0.24 version installation and configuration method graphic tutorial

This article records the installation and configu...

Detailed explanation of the execution process of mysql update statement

There was an article about the execution process ...

Specific steps to use vant framework in WeChat applet

Table of contents 1. Open the project directory o...

Steps to customize icon in Vue

ant-design-vue customizes the use of Ali iconfont...

MySQL 5.7.19 winx64 free installation version configuration tutorial

mysql-5.7.19-winx64 installation-free version con...