简体   繁体   English

Nginx - 在离开安全页面时将HTTPS重定向回HTTP

[英]Nginx - Redirect HTTPS back to HTTP when leaving a secured page

I have Nginx in front of a Node.js app. 我在Node.js应用程序前面有Nginx。 I have it set up so that if the url has /account in it, it'll redirect to HTTPS. 我已将其设置为如果网址中包含/ account,则会重定向到HTTPS。 My question is - how do I set it up so that if the user leaves the /account url (clicks a link to go to the home page), it'll get sent back to HTTP? 我的问题是 - 如何设置它,以便如果用户离开/帐户URL(单击链接转到主页),它将被发送回HTTP?

Here's my ngnix.conf: 这是我的ngnix.conf:

worker_processes  1;

error_log  logs/error.log;

pid        logs/nginx.pid;

events {
    worker_connections  128;
}

http {
    include       mime.types;
    default_type  application/octet-stream;

    sendfile        on;

    server_tokens off;

    #keepalive_timeout  0;
    keepalive_timeout  65;
    tcp_nodelay        on;
    proxy_buffer_size   128k;
    proxy_buffers   4 256k;
    proxy_busy_buffers_size   256k;

    gzip  on;

    server {
        listen       80;
        server_name  localhost;

        location / {
            proxy_set_header x-path $uri;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header Host $http_host;
            proxy_set_header X-NginX-Proxy true;
            proxy_pass http://127.0.0.1:3000;
            proxy_redirect off;
        }

        location /account {
          rewrite ^(.*) https://$host$1 permanent; #redirect to https
        }

        error_page  404              /404.html;

        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }

    }

    server {
        listen 443;
        ssl on;

        ssl_certificate ssl/server.crt;
        ssl_certificate_key ssl/server.key;

        location / {
            proxy_set_header Host $http_host;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header X-M-Secure "true";
            proxy_redirect off;
            proxy_max_temp_file_size 0;
            proxy_pass http://127.0.0.1:3000;
        }
    }
}

Thanks in advance for any assistance. 在此先感谢您的任何帮助。

This is untested. 这是未经测试的。

server {
    listen 443;
    ssl on;

    ssl_certificate ssl/server.crt;
    ssl_certificate_key ssl/server.key;

    location /account/ {
        proxy_set_header Host $http_host;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-M-Secure "true";
        proxy_redirect off;
        proxy_max_temp_file_size 0;
        proxy_pass http://127.0.0.1:3000;
    }

    location / {
        rewrite ^(.*) http://$host$1 permanent; # redirect to http
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM