【问题标题】:Nginx - Redirect HTTPS back to HTTP when leaving a secured pageNginx - 离开安全页面时将 HTTPS 重定向回 HTTP
【发布时间】:2011-10-29 04:41:09
【问题描述】:

我在 Node.js 应用程序前安装了 Nginx。我已经设置好了,如果 url 中包含 /account,它将重定向到 HTTPS。我的问题是 - 我如何设置它,以便如果用户离开 /account 网址(点击链接转到主页),它将被发送回 HTTP?

这是我的 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;
        }
    }
}

提前感谢您的帮助。

【问题讨论】:

    标签: node.js nginx express


    【解决方案1】:

    这是未经测试的。

    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
        }
    }
    

    【讨论】:

    • 谢谢,成功了!现在看来如此明显。我会对此投赞成票,但我需要 15 个代表才能做到这一点!
    猜你喜欢
    • 1970-01-01
    • 2011-03-29
    • 2018-01-05
    • 1970-01-01
    • 2012-12-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-10-11
    相关资源
    最近更新 更多