Score:1

Nginx disable https redirect for 404+http uri

jp flag

I have HTTPS redirect in nginx.conf

  1. But if the requested URI is HTTP and 404 - I don't need a redirect. Is there any solution for this specific configuration?

  2. Also is there a possibility to turn off HTTPS + www redirect for 404 links like http://www.test.com

  3. If point 1 or 2 cannot be done, can I hardcode a list of 404 URLs with nginx to completely turn off any redirect for them and only show 404 page? (HTTPS and www)

WordPress + Nginx (+Vestacp)

server {
    listen      x.x.x.x:80;
    server_name test.com www.test.com;
    root        /home/admin/web/test.com/web/;
    index       index.php index.html index.htm;
    access_log  /var/log/nginx/domains/test.com.log combined;
    access_log  /var/log/nginx/domains/test.com.bytes bytes;
    error_log   /var/log/nginx/domains/test.com.error.log error;
    return 301 https://test.com$request_uri;
}
sv flag
It could be done for static content. For dynamic app, such as WordPress, it may not be possible. What app or kind of app do you run on this specific case? Static or dynamic?
allwillburnn avatar
jp flag
Wordpress + Nginx
Score:0
sv flag

Welcome to ServerFault.

But if the requested URI is HTTP and 404 - I don't need a redirect. Is there any solution for this specific configuration?

I don't know how to do this. I am unsure if it is even possible.

Also is there a possibility to turn off HTTPS + www redirect for 404 links like http://www.test.com

I don't understand this question.

If point 1 or 2 cannot be done, can I hardcode a list of 404 URLs with nginx to completely turn off any redirect for them and only show 404 page? (HTTPS and www)

This is possible. Assuming we have the list of files at /home/admin/web/test.com/web/404.conf in the following format...

/aboutt-us/ 404;
/about-uss/ 404;
/contactt-us/ 404;
/contact-uss/ 404;

Instead of the text "404", we could have anything except "0" (zero).

The modified configuration would look like this...

map $uri $allwillburnn { include /home/admin/web/test.com/web/404.conf; }

server {
    listen      x.x.x.x:80;
    server_name test.com www.test.com;
    root        /home/admin/web/test.com/web/;
    index       index.php index.html index.htm;
    access_log  /var/log/nginx/domains/test.com.log combined;
    access_log  /var/log/nginx/domains/test.com.bytes bytes;
    error_log   /var/log/nginx/domains/test.com.error.log error;
    if ( $allwillburnn ) { return 404; }
    return 301 https://test.com$request_uri;
}

Explanation: By default, for any URLs not listed in the aforementioned 404.conf file, the variable named $allwillburnn is assigned to a value of 0 (zero) by map. In this case, if block is skipped, resulting in 301 redirect.

For URLs listed in 404.conf file, $allwillburnn is assigned to non-zero value. In this case, if block is parsed, resulting in 404.

I hope that helps.

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.