Scene
HTTP was used earlier in the project, and HTTPS was enabled for security reasons later.
Project Architecture: The front end uses Nginx as the reverse proxy and load balancer for multiple Tomcat instances.
In fact, only need to enable HTTPS on the Nginx, so that the client and Nginx after the use of HTTPS communication, and Nginx and Tomcat still communicate in HTTP mode.
Now it is necessary to automatically redirect all HTTP requests from the previous client to HTTPS, just add the appropriate configuration on the Nginx.
The following configuration implementation comes from Nginx HTTP jump to HTTPS, but I have tried to verify.
In addition, also added some of their own understanding organized.
Method 1: Use the rewrite directive
server { listen 80; server_name domain.com; rewrite ^(.*) https://$server_name$1 permanent;}server { listen 443 ssl; server_name domain.com; ssl on; ssl_certificate /etc/nginx/ssl/domain.com.crt; ssl_certificate_key /etc/nginx/ssl/domain.com.crt; # other}
If Nginx is the front-end reverse proxy for Tomcat, you need to place the configuration in the server block where SSL is configured.
Method 2: Use the return command
server { listen 80; server_name domain.com; return 301 https://$server_name$request_uri;}server { listen 443 ssl; server_name domain.com; ssl on; ssl_certificate /etc/nginx/ssl/domain.com.crt; ssl_certificate_key /etc/nginx/ssl/domain.com.crt; # other}
If Nginx is the front-end reverse proxy for Tomcat, you need to place the configuration in the server block where SSL is configured.
Method Three: Using the error_page directive
When HTTP is only allowed to access, HTTP access causes Nginx to report 497 errors, and then uses Error_page to redirect the link to HTTPS.
server { listen 80; listen 443 ssl; server_name domain.com; ssl on; ssl_certificate /etc/nginx/ssl/domain.com.crt; ssl_certificate_key /etc/nginx/ssl/domain.com.crt; # other error_page 497 https://$server_name$request_uri;}
When using the Error_page directive, the listening configuration for HTTP and HTTPS is written in the same server block, and the corresponding other configuration needs to be done in the server configuration block.
Note that it is time to error_page write the instruction statement at the end, otherwise it will not take effect.
Reference: https://www.cnblogs.com/nuccch/p/7681592.html
Nginx HTTP to HTTPS