大网数据广告活动
服务器运维问答

Redirect All URL to SSL, Except for X

· 52CE 我爱测

I run NGINX on Vesta CP. There are two separate files nginx.conf and snginx.conf . Each has a server block in it. I redirect entire sites to SSL. To do this I add a return 301 https://example.com$request_uri; to the nginx.conf , …

问题原文

原帖链接 · ChristopherDill · CC BY-SA 3.0

I run NGINX on Vesta CP. There are two separate files nginx.conf and snginx.conf. Each has a server block in it. I redirect entire sites to SSL. To do this I add a return 301 https://example.com$request_uri; to the nginx.conf, and then I fully configure the sningx.conf for caching and location blocks and everything else.

I do have one domain which is partial SSL/non-SSL. In these files I for not 301, but instead I 301 a location block, like this:

location ~ ^/(wp-login.php|wp-admin|wp-login|shop|product|my-account|checkout-2|order-pay|order-received|add-payment-method|cart) {
return 301 https://domain.com$request_uri;}

And in the SSL block I exclude:

location ~ ^(?!/(wp-login.php|wp-admin|wp-login|shop|product|my-account|checkout-2|order-pay|order-received|add-payment-method|cart)) {
return  301 http://sergerpepper.com$request_uri;}

I have a need to have one URL available on both SSL and non-SSL, with everything else on SSL. This url is the sitemap. So both of these would work:

http://example.com/sitemap_index.xml

https://example.com/sitemap_index.xml

But any other URL would only be on SSL. With my above previous redirect method, I can point a URL at HTTP or Exclude it for HTTPS, but not both.

已采纳解答

原帖链接 · Michael Hampton · CC BY-SA 3.0

Your https server block should not redirect anything to http, as you don't need that in this situation.

Your http server block should redirect everything, except the URL in question:

server {
    listen [::]:80;
    listen 80;
    server_name .example.com;
    root /srv/www/example.com;

    location = /sitemap_index.xml {
        try_files $uri =404;
    }

    location / {
        return 301 https://$host$request_uri$is_args$args;
    }
}

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2016-03-04)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 3.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。