簡體   English   中英

使用重寫https到http重定向無法在nginx配置中工作

[英]https to http redirect not working in nginx configuration using rewrite

為了在我們的應用程序中分配負載並實現安全性,我們從amazon采用了彈性Load Balancer,並在其上配置了SSL。現在,從http到https的重定向在服務器上的nginx配置或ELB上附加的實例上不起作用。 以下是nginx配置: -

 server {
        listen 80 default_server;
        listen [::]:80 default_server ipv6only=on;
        server_name new.example.com;
        access_log /var/log/nginx/domain-access.log;
        location / {
                proxy_read_timeout 90;
                proxy_connect_timeout 90;
                proxy_set_header Host $host;
                proxy_set_header X-Real-IP $remote_addr;
                proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
                proxy_set_header X-Forwarded-Proto $scheme;
                proxy_pass http://127.0.0.1:8000;
              }
}

首先服務器不支持https URL然后我在配置中添加一些代理設置但現在問題是重定向不起作用我在ngnix配置中使用以下命令將http重定向到https: -

#version 1   
    server{
      return         301 https://$server_name$request_uri;
    }

#version 2   
    server {
     rewrite ^(.*) https://$host$1 permanent;
    }

部署在服務器上的應用程序是使用django框架構建的。

當我做了類似的事情時,我已經設置了ELB HTTPS以重定向到節點上的HTTP端口80。 然后我在節點上設置了第二個nginx vhost,例如在端口81上,它指示返回301 https:// $ server_name $ request_uri; 並設置ELB http偵聽器以重定向到該端口(其中$ server_name顯然指向ELB的域CNAME)

然后,我確保無法使用安全組從我的VPC外部訪問ELB背后的實例。

如果您使用Django,您可以使用它來進行重定向,這提供了很多靈活性,例如僅在生產服務器上啟用HTTPS重定向或選擇要重定向的URL:

# Honor the 'X-Forwarded-Proto' header for request.is_secure()
SECURE_PROXY_SSL_HEADER = ('HTTP_X_FORWARDED_PROTOCOL', 'https')

# Redirect to HTTPS in production
SECURE_SSL_REDIRECT = not DEBUG

# Disable redirection on this urls
SECURE_REDIRECT_EXEMPT = [
    '^legacy/api/',
]

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM