How to get nginx to redirect to another path only if the root path is requested?

leeand00 nginx-forum at forum.nginx.org
Fri Jan 26 19:24:16 UTC 2018


How to get nginx to redirect to another path only if the root path is
requested?

Here is part of my server configuration:

server {
        listen     80; ## listen for ipv4; this line is default and implied
    #listen   [::]:80 default_server ipv6only=on; ## listen for ipv6

    # Make site accessible from http://localhost/
    server_name wiki wiki.leerdomain.lan;

    # Note: There should never be more than one root in a 
    #       virutal host
    #   Also there should never be a root in the location.
    #root /var/www/nginx/;

    rewrite ^/$ /rootWiki/ redirect; 


    location ^~ /rootWiki/ {
            resolver 127.0.0.1 valid=300s;
            access_log ./logs/RootWiki_access.log;
            error_log ./logs/RootWiki_error.log;
            proxy_buffers 16 4k;
            proxy_buffer_size 2k;
            proxy_set_header Host $host;
            proxy_set_header X-Real_IP $remote_addr;
            rewrite /rootWiki/(.*) /$1 break;
            proxy_pass http://192.168.1.200:8080;
        }

   location ^~ /usmle/ {
    access_log ./logs/usmle_access.log;

 ...

When I configure it as above I am unable to access any of the sub-locations
under root...but the root directory does forward to /rootWiki/ but I receive
a 502 Bad Gateway instead of the application on port 8080.

When I remove the line:

rewrite ^/$ /rootWiki/ redirect;

I'm able to access the rootWiki application, and all the sub locations from
root just fine.

It seems to me like it should work but it does not appear to.

Posted at Nginx Forum: https://forum.nginx.org/read.php?2,278283,278283#msg-278283



More information about the nginx mailing list