【问题标题】:Excluding specific URL from HTTPS redirect on NGINX vhost从 NGINX 虚拟主机上的 HTTPS 重定向中排除特定 URL
【发布时间】:2016-09-23 16:59:43
【问题描述】:

我正在尝试从 HTTPS 重定向中排除特定 URL(所有流量都从 HTTP 重定向到 HTTPS,因此我希望有一个 URL 可通过 HTTP 访问)。

要排除的网址:

http://www.domain.com/index.php?route=extension/something/something

我的 VHOST domain.com.conf:

server{
listen      80;
listen      443 ssl;
server_name domain.com www.domain.com;
ssl         on;

if ( $scheme = "http" ) {
rewrite ^/(.*)$ https://$host/$1 permanent;
}
index index.php index.html;

root /var/www/domain.com;

    keepalive_timeout   60;
    ssl_certificate      /etc/nginx/ssl/domain.com.crt;
    ssl_certificate_key  /etc/nginx/ssl/domain.com.key;

location ~ \.php$ {
try_files $uri = 404;
include fastcgi_params;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param HTTPS on;
}

location /image/data {
        autoindex on;
    }

location /upload {
        autoindex on;
        allow all;
        log_not_found off;
    }

location /admin {
        index index.php;
    }

location / {
        try_files $uri @opencart;
    }

location @opencart {
        rewrite ^/(.+)$ /index.php?_route_=$1 last;
    }

location = /favicon.ico {
        log_not_found off;
        access_log off;
    }

location = /robots.txt {
        allow all;
        log_not_found off;
        access_log off;
    }
location ~* \.(xml|csv|xls)$ {
    allow all;
    log_not_found off;
}

location ~* \.(engine|inc|info|install|make|module|profile|test|po|sh|.*sql|theme|tpl(\.php)?|xtmpl)$|^(\..*|Entries.*|Repository|Root|Tag|Template)$|\.php_ {
        deny all;
    }

location ~ /\. {
        deny all;
        access_log off;
        log_not_found off;
    }

location ~*  \.(jpg|jpeg|png|gif|css|js|ico)$ {
        expires max;
        log_not_found off;
    }
}

我用谷歌搜索了所有内容并尝试了这里的所有提示,但均未成功。

【问题讨论】:

    标签: nginx url-rewriting


    【解决方案1】:

    请尝试以下代码,

    server {
       ...
       set $is_redirect "0";
       if ( $scheme = "http" ) {
          set $is_redirect "1";
       }
       if ($arg_route ~* "extension/something/something") {
          set $is_redirect "0";
       }
       if ($is_redirect) {
          rewrite ^/(.*)$ https://$host/$1 permanent;
       }
       ...
    }
    

    【讨论】:

    • 感谢您的出色解决方案。我还有一个问题:如何为这些情况添加重写以也返回 HTTP?最后重写 ^/googlebase.xml$ /index.php?route=feed/google_base;
    • 我认为你想在 http 处理期间进行重写,试试 if ( $scheme = "http" ) { rewrite ^/googlebase.xml$ /index.php?route=feed/google_base last; } 在第三个 if 块下方。
    猜你喜欢
    • 2016-06-06
    • 1970-01-01
    • 2013-02-05
    • 2018-09-23
    • 2019-06-18
    • 1970-01-01
    • 2016-07-16
    • 2019-08-13
    • 1970-01-01
    相关资源
    最近更新 更多