ngnix https redirect with querystring

时间:2017-01-18 07:24:17

标签: http redirect nginx https

我已经在我的服务器上安装了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 {         server 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;
    }
}

1 个答案:

答案 0 :(得分:1)

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

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

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