Score:0

Nginx not listening at local address

pk flag

When I go to localhost on my pc, I can connect, but when I go to my router's public ip on the host pc, the page gets timed out. It works on my phone and I am able to see the website.

Here is my nginx configuration: (I've replaced the listen address with ***):

server {
                listen 80;
                server_name ***;
                index index.html index.php;

                access_log /var/log/nginx/localhost.access_log main;
                error_log /var/log/nginx/localhost.error_log info;

                root /var/www/localhost/htdocs;
                location ~ \.php$ {
                        try_files $uri =404;
                        fastcgi_pass unix:/var/run/php-fpm/php-fpm.sock;
                        fastcgi_index index.php;
                        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
                        include fastcgi_params;
                }

                listen 443 ssl; # managed by Certbot
                ssl_certificate /etc/letsencrypt/live/***/fullchain.pem; # managed by Certbot
                ssl_certificate_key /etc/letsencrypt/live/***/privkey.pem; # managed by Certbot
                include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
                ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot
                if ($scheme != "https") {
                    return 301 https://$host$request_uri;
                } # managed by Certbot
                #return 404; # managed by Certbot
}
br flag
Have you configured your router properly?
RadoslavL avatar
pk flag
Yes, I have set up port forwarding and it is working properly, but I can't access my server on my host machine using the public ip. Localhost connection works as well as connecting from a different device.
br flag
Does this answer your question? [Loopback to forwarded Public IP address from local network - Hairpin NAT](https://serverfault.com/questions/55611/loopback-to-forwarded-public-ip-address-from-local-network-hairpin-nat)
Score:0
in flag

There is some information missing to be sure what solves you problem.

My guess is that you need the set the "default server" to get something from localhost. The servername option tells nginx to only listen for requests going to the configured IP-Name. Usually localhost is not accessed by the servername.

listen      80 default_server;

From nginx manual (http://nginx.org/en/docs/http/request_processing.html):

In this configuration nginx tests only the request’s header field “Host” to determine which server the request should be routed to. If its value does not match any server name, or the request does not contain this header field at all, then nginx will route the request to the default server for this port.

RadoslavL avatar
pk flag
The solution you provided should work, but even when I connect to the page using the hostname from the server_name, it doesn't work. I can provide more information.
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.