简体   繁体   中英

Basic nginx proxy_pass configuration doesn't work

I have this basic nginx configuration

http {
    server {
        location / {
            proxy_pass http://localhost:8080;
        }
    }
    server {
        listen 8080;
        root /data/upl;
    }
}

I also have index.html on /data/upl on nginx.

When i go to http://localhost - i get back the HTML in Chrome.

After i'm changing the location matcher from / to /test/ and go to http://localhost/test I get back error 404 of nginx in chrome.

that's the updated configuration:

http {
    server {
        location /test/ {
            proxy_pass http://localhost:8080;
        }
    }
    server {
        listen 8080;
        root /data/upl;
    }
}

Please help with understanding this issue (why it doesn't work).

I think that configuration may be too basic. You're using a forward slash in the location, which means you also need to tell nginx what type of file(s) to look for by using the index directive.

Excerpt from documentation

If a request ends with a slash, NGINX treats it as a request for a directory and tries to find an index file in the directory. The index directive defines the index file's name (the default value is index.html). To continue with the example, if the request URI is /images/some/path/, NGINX delivers the file /www/data/images/some/path/index.html if it exists. If it does not, NGINX returns HTTP code 404 (Not Found) by default

I don't understand why you are using a proxy, doesn't seem needed. Here is the configuration I would suggest using.

server {
  listen 8080;
  root /data/upl;
  index index.htm index.html;

  location /test {
  }
}

There will need to be an index.htm or index.html file in /data/upl/test or a 404 will be generated.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM