Redirect All URL to SSL, Except for X

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.


Solution 1:

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;
    }
}