【问题标题】:ngnix https redirect with querystring带有查询字符串的 nginx https 重定向
【发布时间】:2017-06-02 11:37:57
【问题描述】:

我已经在我的服务器上安装了 ssl,它工作正常,但问题是,如果任何用户尝试在没有 https 的情况下访问站点,那么他/她会在没有查询字符串的情况下重定向到 https。

http://example.com?av=23423423https://example.com 只是它不使用查询字符串重定向。我尝试添加以下代码,但它不起作用。

if ($http_x_forwarded_proto = "http") {
      return 301 https://$server_name$request_uri;
  }

我的conf文件在下面,有人可以帮忙吗?

上游 mysitecombackend { 服务器 unix:/var/run/php-fcgi-mysitecom.sock; }

upstream exmplecombackend {
        server unix:/var/run/php-fcgi-exmplecom.sock;
}

server {
    listen 1.2.4.3:443 ssl;
    server_name exmple.com *.exmple.com;      
    ssl on;
    ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
    ssl_ciphers AES256+EECDH:AES256+EDH:!aNULL;
    ssl_prefer_server_ciphers on;

    ssl_certificate    /etc/ssl/www.exmple.com.cabundle;
    ssl_certificate_key    /etc/ssl/exmple.key;


   location / {
        proxy_pass http://127.0.0.1:80;
        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 https;
        proxy_set_header X-Forwarded-Port 443;
       # proxy_set_header Host $host$request_uri;
        proxy_set_header Host $host;
        proxy_set_header Ssl-Offloaded "1";
    }
#rewrite ^/(.*) https://exmple.com/$1 permanent;
}

server {
    listen 80 default_server;
    server_name exmple.com www.exmple.com;
   # return 301 https://$host$request_uri;
    root /var/www/vhosts/exmple.com/public;

    location / {
        index index.html index.php;
        try_files $uri $uri/ @handler;
        expires 30d;
    }

    location /app/                { deny all; }
    location /includes/           { deny all; }
    location /lib/                { deny all; }
    location /media/downloadable/ { deny all; }
    location /pkginfo/            { deny all; }
    location /report/config.xml   { deny all; }
    location /var/                { deny all; }

    location  /. {
        return 404;
    }

    location @handler {
        rewrite / /index.php;
    }

    location ~ .php/ {
        rewrite ^(.*.php)/ $1 last;
    }

    include "ssl_offloading.inc";
    location ~ .php$ {
        if (!-e $request_filename) { rewrite / /index.php last; }

        expires        off;
        fastcgi_pass   exmplecombackend;
        fastcgi_param  HTTPS $fastcgi_https;
        fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
#       fastcgi_param  MAGE_RUN_CODE default;
#       fastcgi_param  MAGE_RUN_TYPE store;
        include        fastcgi_params;
    }
}

【问题讨论】:

    标签: http redirect nginx https


    【解决方案1】:

    您从未将$http_x_forwarded_proto 设置为http,因此if 条件始终为假。

    您可以尝试将变量与https 进行比较,例如:

    if ($http_x_forwarded_proto != "https") {
        return 301 https://$server_name$request_uri;
    }
    

    【讨论】:

      猜你喜欢
      • 2019-06-17
      • 2017-04-10
      • 2017-04-17
      • 2019-01-03
      • 2016-01-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多