Nginx HTTP to HTTPS

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.