One day, the company’s service switched domain names to disable HTTP and allow access only through HTTPS. When you enter the url in the browser address bar to access the static resource directory, you will find the default HTTP protocol address. The network topology is as follows:

As shown in the figure above, the client HTTPS request reaches the API gateway, which then forwards the request to the static resource server over the HTTP protocol.

The browser finds that a 301 status code is received in response to the HTTPS request sent by the client, and the Location field in the response header is the address of the HTTP protocol to be jumped to.

1. Cause analysis

Why does the server return 301? First, you need to understand the meaning of the status code. All status response codes starting with 3xx in HTTP are redirected responses. According to the DEFINITION of RFC:

301 Moved Permanently

302 Found

303 See Other

307 Temporary Redirect

301 is a permanent redirect. When using Nginx as an HTTP server, there are basically two situations when a user enters a nonexistent address: 1. Return the 404 status code. 2. Return the 301 status code and the redirect address. 404 Not Found: 301 Moved Permanently.

The first question to clarify is, under what circumstances will a 301 redirect be triggered?

Nginx is responsible for setting the 301 Moved Permanently status code. But nginx.conf controls how nginx handles 301 Moved Permanently status code! In other words, whether or not the page is redirected, and how it is redirected, is a matter of user configuration! Nginx actively set 301 Moved Permanently. There is only one case where the user enters a URL in the browser that does not end with a slash or a file directory, for example, www.test.com/index. Nginx did not find the file index, but found that index is a directory. The return status code for this visit will be set to 301 Moved Permanently.

The communication between the browser and Nginx is as follows:

Typically, we configure absolute_redirect, server_name_IN_redirect, and port_in_redirect in nginx.conf to achieve personalized redirection. Absolute_redirect controls how the Location URL is generated.

  • Absolute_redirect set to on generates the absolute Location URL.
  • Absolute_redirect set to off generates the relative path as the Location URL.

The other two configurations take effect only if absolute_redirect is set to ON.

  • If the server_name_in_redirect is on, use server_name in the Nginx configuration file as host in the Location URL; otherwise, use the host name in the user request URL as host.
  • If port_in_redirect is set to on, use the port that nginx listens to to construct the Location URL, otherwise port is not set.

Therefore, the three configuration items control the results of the Location URL, for example, Location: http://server_name:port/index/. The default values for these three configurations are absolute_redirect= ON, server_NAMe_IN_redirect =off, and port_IN_redirect = ON. So the default Location URL will be “www.test.com/index/”.

Nginx, as a static resource server, has a 301 status code, and since it listens on port 80 and uses HTTP, assuming the browser accesses “www.test.com/index”, The default constructed Location URL is “www.test.com/index/”, and the API gateway writes the response directly back to the browser. The browser receives a 301 response, parses the Location value of the response header, and jumps. The process is shown in the figure below:

2. Solutions

The easiest way to do this is to set absolute_redirect to off and construct the relative Location URL as shown in the following example:

    server {
        listen 80; Absolute_redirect off absolute_redirect off server_name localhost; charset utf- 8 -;
        access_log logs/access.log main;
        error_log logs/error.log;

        large_client_header_buffers 4 16k;
        client_max_body_size 300m;
        client_body_buffer_size 128k; location / { index index.html; root /var/www/index/; }}Copy the code

Then, for the request “www.test.com/index”, the Location response header will be equal to /index/.