Port_in_redirect configuration for nginx

  nginx

Order

This article mainly explains the practical application of port_in_redirect.

Scene

There is an 80-port nginx. To forward a path to another 8080-port nginx, the configuration is as follows

server {
        listen       80  default_server;
        server_name demoapp.com.cn;
        location /public/ {
            proxy_pass http://192.168.99.100:8080/public/ ;
        }
}

The configuration of another nginx is as follows

server {
        listen       8080  default_server;
        location ~* /public/(share|webview) {
            root   html ;
            proxy_buffering off;
            index  index.html index.htm;
        }
}

The html directory contains a public directory, and the public directory contains share and webview directories to store the static resources of each sub-module.

Problem

After this configuration, when visiting through demoapp.com.cn/public/share, you will jump to demoapp.com.cn:8080/public/share.

Assuming that the two nginx listen to the same ip, if not the same ip, it is estimated that server_name will be configured and server_name_in_redirect will be turned on

At this time, port_in_redirect will come in handy.

server {
        listen       8080  default_server;
        location ~* /public/(share|webview) {
            root   html ;
            proxy_buffering off;
            port_in_redirect off;
            index  index.html index.htm;
        }
}

By specifying port_in_redirect off; Tell nginx not to bring port when redirect. If there is no configuration, the default value is true

doc