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

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

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

Andrzej Walas
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 <a href="http://192.168.1.200:8080;">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

_______________________________________________
nginx mailing list
[hidden email]
http://mailman.nginx.org/mailman/listinfo/nginx
Reply | Threaded
Open this post in threaded view
|

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

Francis Daly
On Fri, Jan 26, 2018 at 02:24:16PM -0500, leeand00 wrote:

Hi there,

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

I don't understand the rest of your mail; perhaps if you could show one
request that you make and the response that you get, and how it is not
the same as the response that you want, that would be clearer.

But this first question, I do understand.

  location = / { return 301 /rootWiki/; }

should do what you want.

Good luck with it,

        f
--
Francis Daly        [hidden email]
_______________________________________________
nginx mailing list
[hidden email]
http://mailman.nginx.org/mailman/listinfo/nginx
Reply | Threaded
Open this post in threaded view
|

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

Andrzej Walas
I have other subfolders in my location paths so for instance other than just
/ after my host, I also have /lang/english/grammar, and
/lang/spanish/gramática

But I figured it out:

        location = / {
               resolver 127.0.0.1 valid=300s;
            proxy_pass http://192.168.1.200:8080/;
               proxy_set_header X-Forwarded-Host $host;
               proxy_set_header X-Forwarded-Server $host;
               proxy_set_header X-Real-IP $remote_addr;
               proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
               rewrite /(.*) /$1 break;
               access_log ./logs/root_access.log;
               error_log ./logs/root_error.log;
        }

The = / that you suggested fixed it right up, thanks!

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

_______________________________________________
nginx mailing list
[hidden email]
http://mailman.nginx.org/mailman/listinfo/nginx