Score:0

NGINX - reverse proxy but one subfolder to static local directory

mt flag

I'm having trouble figuring out the basic syntax for proxying most traffic to a local Solr install but leaving one path/dir requests being sent to a static html directory. Do all secondary, tertiary, etc... locations need to be set by regex? Haven't been able to find a simple guide on this.

current errors: /adv/index.html - 404 - Not found /adv/ - 403 Forbidden /solr/ - works fine

Here is my config:

user www-data;
worker_processes auto;
pid /run/nginx.pid;
#include /etc/nginx/modules-enabled/*.conf;

events {
        worker_connections 1024;
        multi_accept on;
}

http {
        sendfile on;
        tcp_nopush on;
        tcp_nodelay on;
        keepalive_timeout 65;
        types_hash_max_size 2048;
        index index.html;

        include /etc/nginx/mime.types;
        default_type application/octet-stream;
        server {
                listen 80;

                location / {
                    proxy_pass http://localhost:8983;
                }

                location /adv/ {
                        # also wasn't sure what permissions needed here
                        # recursively set adv folder and files within to 777
                        alias /var/www/adv;
                }
        }
}
Richard Smith avatar
jp flag
Check the error log, but also try `root /var/www;` instead of `alias /var/www/adv;`
Ivan Shatsky avatar
gr flag
@Tyndall When you use an `alias` directive inside the location, you need to use slashes carefully. Your configuration will replace the `/adv/` prefix of your URI with the `/var/www/adv` resulting in invalid filename. [Here](https://stackoverflow.com/a/69296739/7121513) is more detailed explanation with examples.
mt flag
weird. based off the logs its seems that my requests were either trying to get to /adv/adv/index.html when I used root and /advindex.html when I used alias.
mt flag
If I try to use root and remove the adv from the path (letting it add itself from the url I pass) it works for everything except /adv, almost like it doesn't know its a directory and to just serve my index.html file. (/adv/ works and /adv/index.html works)
Ivan Shatsky avatar
gr flag
@Tyndall This is an expected behavior. You can force a redirect using something like `location = /adv { return 301 /adv/; }`. Use either `root /var/www` or `alias /var/www/adv/` inside the `location /adv/ { ... }`, but not the `alias /var/www/adv`. Using `root /var/www/` won't do such a harm since it will result in a `/var/www//adv/index.html` file path which would work too.
Ivan Shatsky avatar
gr flag
@Tyndall Or you can use either `location /adv { root /var/www; }` or `location /adv { alias /var/www/adv; }`, however it will overtake any request URI starting with `/adv`, e.g. `/advance`, too.
mt flag
thanks everyone. So the solution that seems to be working for me. Is from Ivan's first comment (and Richard) is alias with no slashes on location path or alias path. Weird thing is ... when I type in /adv without the slash I think I'm getting redirected to a version of the url with the ending slash. Is that normal for Nginx to do this?
Ivan Shatsky avatar
gr flag
@Tyndall I think it is normal for every web server since `adv` is a directory and not a file. You are viewing an index inside the `adv` directory. Think of it like it is a filesystem.
mt flag
Should add this as an answer to get credit. Thanks again.
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.