Score:0

Raspberry pi running nginx responds 200 for localhost, 404 when accessed through hostname

cn flag
dma

I have set up a vanilla installation of nginx on a raspberry pi.

server configuration:

server {
    listen 80;
    listen [::]:80;

    root /home/pi/www;

    index index.html index.htm;

    server_name _;

    location / {
        # return 418 on not found. non standard but a useful test
        try_files $uri $uri/ =418;
    }
}

curling through localhost, I get a response. curling the hostname I get 404. There is no entry in the access log for the latter, only the former:

pi@pik3s $ hostname
pik3s
pi@pik3s $ curl localhost
<h1>HELLO</h1>
pi@pik3s $ curl pik3s
404 page not found
pi@pik3s $ tail /var/log/nginx/access.log
::1 - - [22/Jun/2021:13:51:32 +0100] "GET / HTTP/1.1" 200 15 "-" "curl/7.64.0"

I don't understand where the 404 is coming from as I'm returning 418 for not found as a test, and the 404 doesn't seem to hit nginx at all.

updated: 2021-06-23: i set the error log to 'debug' and this is the contents:

$ cat /var/log/nginx/error.log
2021/06/23 10:53:00 [debug] 15325#15325: epoll add event: fd:6 op:1 ev:10000001
2021/06/23 10:53:00 [debug] 15325#15325: epoll add event: fd:7 op:1 ev:10000001
2021/06/23 10:53:00 [debug] 15326#15326: epoll add event: fd:6 op:1 ev:10000001
2021/06/23 10:53:00 [debug] 15326#15326: epoll add event: fd:7 op:1 ev:10000001
2021/06/23 10:53:00 [debug] 15328#15328: epoll add event: fd:6 op:1 ev:10000001
2021/06/23 10:53:00 [debug] 15328#15328: epoll add event: fd:7 op:1 ev:10000001
2021/06/23 10:53:00 [debug] 15330#15330: epoll add event: fd:6 op:1 ev:10000001
2021/06/23 10:53:00 [debug] 15330#15330: epoll add event: fd:7 op:1 ev:10000001

ie, the request seems to be stopping before it gets to nginx.

updated: 2021-06-25: checked curl -v and localhost returns Server: nginx/1.14.2 while pik3s returns no Server.

I realised that /etc/hosts only resolves pik3s to an IPv4 address, so I added ::1 as an alternative, and now it works (on the local machine).

So this suggests that nginx is only listening on IPv6.

$ sudo netstat -ltnp | grep :80
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      26923/nginx: master 
tcp6       0      0 :::80                   :::*                    LISTEN      26923/nginx: master 

but it is listening.

Michael Hampton avatar
cz flag
That error `404 page not found` did not come from nginx, it came from a web application written in Go.
cn flag
dma
there's nothing else listening on port 80: sudo netstat -ltnp | grep -w ':80' --> tcp6 0 0 :::80 :::* LISTEN 5361/nginx: master
Nikita Kipriyanov avatar
za flag
Please notice, 404-s and other error conditions should be registered in the `error.log`. What's there?
cn flag
dma
added an update with error log above
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.