nginx发送301重定向到http而不是https

时间:2018-08-08 21:44:41

标签: nginx

在Docker容器中运行nginx 1.11.6

我有许多重定向,格式为/old/content-> /new/content

但是nginx会将来自https的访问者重定向到httphttps://example.com/old/content-> http://example.com/new/content

我有一个重定向规则,将所有http流量重定向回https,但这会创建一个重定向链,我正在尝试消除该重定向链。

是否有一种方法可以强制nginx单独保留网址的协议部分?

这是我的配置示例:

gzip on;
gzip_disable "msie6";

gzip_vary on;
gzip_proxied any;
gzip_comp_level 6;
gzip_buffers 16 8k;
gzip_http_version 1.1;
gzip_min_length 256;
gzip_types 
    text/plain text/css text/js text/javascript text/xml
    application/json application/javascript application/x-javascript
    application/xml application/xml+rss
    application/x-pdf application/pdf;

rewrite ^/(.*)/$ /$1 permanent;
proxy_intercept_errors on;
error_page 404 /404;

if ($http_x_forwarded_proto != "https") {
   rewrite ^(.*)$ https://$server_name$1 permanent;
}

location /old/content {
   rewrite ^/.* /new/content permanent;
}

location /old/content2 {
   return 301 /new/content2;
}

location /old/content3 {
   return 302 /new/content3;
}

1 个答案:

答案 0 :(得分:0)

使用 let top5 = Array(labels.sorted{ $0.confidence > $1.confidence}.prefix(upTo:5)) var tmparr : [] var count: Int = 0 for lulu in top5 { let translator = ROGoogleTranslate() count = count + 1 var params = ROGoogleTranslateParams() params.source = "en" params.target = "es" params.text = "\(String(describing: lulu.label))" params.ordernumber = count translator.translate(params: params, callback: { (result) in tmparr.append[params] }) } DispatchQueue.main.async { for lulunew in tmparr { if (lulunew.ordernumber == correctindex){ self.textUno.text = self.textUno.text! + "\(lulu.label)" + " \(lulu.confidence*100)\n" self.textDos.text = self.textDos.text! + "\(result)\n" self.view.addSubview(self.textUno) self.view.addSubview(self.textDos) } } } 在本地修改了nginx配置,这是一个有效的版本:

nginx:1.11.6

请注意:

    如果要将流量从http重定向到https,建议使用{li> error_page 404 /404; server { listen 80; return 301 https://$host$request_uri; } server { listen 443 ssl; ssl on; ssl_certificate /tmp/ssl.pem; ssl_certificate_key /tmp/ssl-key.pem; ssl_session_timeout 5m; ssl_session_cache shared:SSL:10m; ssl_protocols TLSv1 TLSv1.1 TLSv1.2; ssl_ciphers 'EECDH+AESGCM:EDH+AESGCM:AES256+EECDH:AES256+EDH'; ssl_prefer_server_ciphers on; proxy_intercept_errors on; rewrite ^/(.*)/$ /$1 permanent; location /old/content { rewrite ^/.* /new/content permanent; } location /old/content2 { return 301 /new/content2; } location /old/content3 { return 302 /new/content3; } } ,而不是return 301
  • 在您的用例中,建议将两个服务器块分开,一个用于http,另一个用于https