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

时间:2011-10-28 20:31:47

标签: node.js nginx express

我在Node.js应用程序前面有Nginx。我已将其设置为如果网址中包含/ account,则会重定向到HTTPS。我的问题是 - 如何设置它,以便如果用户离开/帐户网址(点击链接转到主页),它将被发送回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;
        }
    }
}

提前感谢您的任何帮助。

1 个答案:

答案 0 :(得分:8)

这是未经测试的。

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