nginx agent configuration

  1. Simplest reverse proxy configuration
upstream my_server {                                                         
    server 10.0.0.2:8080;                                                
    keepalive 2000; 
}
server {
    listen       80;                                                         
    server_name  10.0.0.1;                                               
    client_max_body_size 1024M;

    location /my/ {
        proxy_pass http://my_server/;
        proxy_set_header Host $host:$server_port;
    }
}

With this configuration, you can access the nginx address http://10.0.0.1:80/my Your request will be forwarded to my_server service address http://10.0.0.2:8080/ .
It should be noted that if the configuration is as follows:

upstream my_server {                                                         
    server 10.0.0.2:8080;                                                
    keepalive 2000;
}
server {
    listen       80;                                                         
    server_name  10.0.0.1;                                               
    client_max_body_size 1024M;

    location /my/ {
        proxy_pass http://my_server;
        proxy_set_header Host $host:$server_port;
    }
}

Then, visit the nginx address http://10.0.0.1:80/my Your request will be forwarded to my_server service address http://10.0.0.2:8080/my . This is because proxy_ If the path of url is not included in the pass parameter, the path identified by the pattern of location will be taken as the absolute path.
2. Redirect message proxy
Even if nginx proxy is configured, when the service returns a redirect message (http code 301 or 302), the redirect target url address will be placed in the location field of the header of the http response message. When the user browser receives the redirection message, it will parse the field and jump. At this time, the new request message will be sent directly to the service address instead of the nginx address. In order for nginx to intercept such requests, the location information of the redirection message must be modified.

location /my/ {
    proxy_pass http://my_server;
    proxy_set_header Host $host:$server_port;

	proxy_redirect / /my/;
}

Using proxy_redirect can modify the location field of the redirection message. In the example, all URLs under the root path will be proxy to the / my / path of nginx and returned to the user. For example, if the original location value of the redirection message returned by the service is / login, the location field of the message received by the user after passing through the nginx agent is / my/login. At this point, the browser will jump to the / my/login address of nginx for access.

It should be noted that the location field of the redirection message returned by the service sometimes fills in the absolute path (including the ip / domain name and port of the service), and sometimes fills in the relative path. At this time, it needs to be screened according to the actual situation.

location /my/ {
    proxy_pass http://my_server;
    proxy_set_header Host $host:$server_port;

	proxy_redirect http://my_server/ http://$host:$server_port/my/;
}

The above configuration is my_ All paths under the root path of the server service are proxied to the / my / path of the nginx address. When nginx is configured with only one server, http:// h o s t : host: host:server_ The port prefix can be omitted.
3. Message data replacement
The worst case of using nginx proxy is that the service address or web absolute path is written in the http response message. It is rare to write the service address, but it also exists occasionally. The most difficult thing is to write the absolute path of the web, especially the absolute path has no public prefix. For example:

General web pages contain paths similar to the following:

  • /Public: used for static page resources, such as js script / public/js, stylesheet / public/css, picture / public/img, etc.
  • /static: similar to / public.
  • /API: API interface for background service.
  • /Login: used for login authentication.
  • other.
    For such services, the possible agent configurations are as follows:
location /my/ {
    proxy_pass http://my_server/;
    proxy_set_header Host $host:$server_port;

	proxy_redirect / /my/;
}
location /login/ {
    proxy_pass http://my_server/public;
    proxy_set_header Host $host:$server_port;
}
location /public/ {
    proxy_pass http://my_server/public;
    proxy_set_header Host $host:$server_port;
}
location /api/ {
    proxy_pass http://my_server/api;
    proxy_set_header Host $host:$server_port;
}

Because similar absolute paths are written in web pages or static resources, users will request the path corresponding to nginx service when jumping through the link in the page. Once there is another service that also contains a similar path and needs nginx to proxy, the contradiction arises: which service is the request under the same path to access nginx forwarded to?

To solve this problem, users must add a uniform prefix to the absolute paths contained in the message data before receiving the message, such as / my/public, / my/api, / my/login, so that the nginx agent configuration can be simplified as follows:

location /my/ {
    proxy_pass http://my_server/;
    proxy_set_header Host $host:$server_port;

	proxy_redirect / /my/;
}
location /other/ {
    proxy_pass http://other_server/;
    proxy_set_header Host $host:$server_port;

	proxy_redirect / /other/;
}

Nginx_ http_ sub_ Module module provides a similar message data replacement function. This module will not be installed by default. You need to add – with HTTP when compiling nginx_ sub_ Module parameter, or directly download the rpm package of nginx.

Using sub_ The syntax for the filter to replace packets is as follows:

location /my/ {
	proxy_pass http://my_server/;
	proxy_set_header Host $host:$server_port;
	
	sub_filter 'href="/' 'href="/my/';
	sub_filter 'src="/' 'src="/my/';
	sub_filter_types text/html;
	sub_filter_once  off;
}

The above configuration will replace the contents of all response messages under / my / with href="/my, and src =" / with src="/my, that is, add a public prefix to all absolute paths.

Note that if you need to configure more than one sub_filter, you must ensure that nginx is above version 1.9.4.

Keywords: Operation & Maintenance Nginx http

Added by kanenas.net on Thu, 09 Dec 2021 02:39:47 +0200