0

Problem: Nginx doesn't route traffic based on the rule I have defined in a separate config file, and just displays the default 404 response.

Context: I have a small middleware application written in Go that provides a simple response to GET requests. The application is deployed on port 8080:

$ curl localhost:8080
ok

I wish to write an Nginx configuration that allows me to route calls from /api to localhost:8080, which would allow me to do the following

$ curl localhost/api
ok

To achieve this, I have written the following config:

/etc/nginx/sites-available/custom-nginx-rules

server {
    listen 80;

    location /api {
        proxy_pass http://localhost:8080;
    }
}

I have also created a softlink in /etc/nginx/sites-enabled/ for the above file

$ ls -l /etc/nginx/sites-enabled
total 0
lrwxrwxrwx 1 root root 34 Jan 19 16:42 default -> /etc/nginx/sites-available/default
lrwxrwxrwx 1 root root 32 Feb 20 14:56 custom-nginx-rules -> /etc/nginx/sites-available/custom-nginx-rules

The rest of the setup is vanilla Nginx, nothing is changed. Despite this simple setup, I get a 404 when making the following call:

$ curl localhost/api
<html>
<head><title>404 Not Found</title></head>
<body bgcolor="white">
<center><h1>404 Not Found</h1></center>
<hr><center>nginx/1.10.3</center>
</body>
</html>

Other info: the following are nginx packages installed on my system (running on raspberry pi)

$ dpkg -l | grep nginx

ii  libnginx-mod-http-auth-pam      1.10.3-1+deb9u1              armhf        PAM authentication module for Nginx
ii  libnginx-mod-http-dav-ext       1.10.3-1+deb9u1              armhf        WebDAV missing commands support for Nginx
ii  libnginx-mod-http-echo          1.10.3-1+deb9u1              armhf        Bring echo and more shell style goodies to Nginx 
ii  libnginx-mod-http-geoip         1.10.3-1+deb9u1              armhf        GeoIP HTTP module for Nginx
ii  libnginx-mod-http-image-filter  1.10.3-1+deb9u1              armhf        HTTP image filter module for Nginx
ii  libnginx-mod-http-subs-filter   1.10.3-1+deb9u1              armhf        Substitution filter module for Nginx
ii  libnginx-mod-http-upstream-fair 1.10.3-1+deb9u1              armhf        Nginx Upstream Fair Proxy Load Balancer
ii  libnginx-mod-http-xslt-filter   1.10.3-1+deb9u1              armhf        XSLT Transformation module for Nginx
ii  libnginx-mod-mail               1.10.3-1+deb9u1              armhf        Mail module for Nginx
ii  libnginx-mod-stream             1.10.3-1+deb9u1              armhf        Stream module for Nginx
ii  nginx                           1.10.3-1+deb9u1              all          small, powerful, scalable web/proxy server
ii  nginx-common                    1.10.3-1+deb9u1              all          small, powerful, scalable web/proxy server - common files
ii  nginx-full                      1.10.3-1+deb9u1              armhf        nginx web/proxy server (standard version)

I also require that this setup is independent of any host or server names. It should do the routing regardless of host.

Running the nginx -T command confirms that the files is loaded. The error log is also empty.

0

Remove the symlink default from /etc/nginx/sites-enabled and restart nginx.

Otherwise this default configuration is used to serve the request before custom-nginx-rules is

  • Is there no way to keep the default? I wish to only append my changes to the existing setup. And I cannot understand why the default configuration kicks in because, after reading the NGinx docs, the location settings with the most specific path are used, which is not happening in my case. – Gabriel Ruiu Feb 21 '18 at 13:44
  • 1
    nginx selects the virtual host first, and the virtual host with default_server is used when no other virtual host matches. When a virtual host is selected, nginx starts looking into the location blocks for that virtual host. Therefore your requests end up to the default virtual host and nginx never sees your location block. – Tero Kilkanen Feb 21 '18 at 23:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.