NGINX default site works, but one on another port does not

Viewed 3839

Hello I am using nginx to set up two servers. one being a frontend port 80 and one being a backend port 8080 on one IP address.

The default one works, the backend through port 8080 gives me a Connection Timeout/ No response when trying to access the server.

I think it is my execution however, to test backend, I have a simple index.php that should come up if i go to xxx.xxx.xxx.xxx:8080 in a browser, except i get a connection time out / no response.

Nginx says it is open and listening. without the port number it goes to my frontend/default server for this ip address.

Error logs show nothing! Even the logs in my server blocks with access and upstream show nothing.

ubuntu 18.04

here is my config server 1 port 80 default

   server {
  # Replace this port with the right one for your requirements
  listen 80 default_server;

  # Multiple hostnames separated by spaces.  Replace these as well.
  server_name www.domain.com domain.com *.domain.com;

  root /var/www/my_project/html;

  error_page 404 errors/404.php;
#  access_log logs/my_project.ventures.access.log;
  access_log  /var/log/nginx/server.intra-upstream.log upstream buffer=16k;
  access_log  /var/log/nginx/server.intra-access.log combined buffer=16k;

  index index.php index.html index.htm;

  # static file 404's aren't logged and expires header is set to maximum age
  location ~* \.(jpg|jpeg|gif|css|png|js|ico|html)$ {
    access_log off;
    expires max;
  }

  location ~ \.php$ {
    include fastcgi.conf;
    fastcgi_intercept_errors on;
    # By all means use a different server for the fcgi processes if you need to
    fastcgi_pass   127.0.0.1:9000;
  }

  location ~ /\.ht {
    deny  all;
  }
}

This is the second server that cannot be accessed via client.domain2.com or www.domain2.com - URL is correct, but shows server 1 htdocs instead. client.domain2.com:8080 or any other combination brings Cannot reach site or Connection Timed out

    server {
  # Replace this port with the right one for your requirements
  listen 8080;

  # Multiple hostnames separated by spaces.  Replace these as well.
  server_name client.domain2.com www.domain2.com;

  root /var/www/my_project/backend;

  error_page 404 errors/404.php;
#  access_log logs/my_project.fun.access.log;
  access_log  /var/log/nginx/server.intra-upstream.log upstream buffer=16k;
  access_log  /var/log/nginx/server.intra-access.log combined buffer=16k;
  index index.php index.html index.htm;

  # static file 404's aren't logged and expires header is set to maximum age
  location ~* \.(jpg|jpeg|gif|css|png|js|ico|html)$ {
    access_log off;
    expires max;
  }

  location ~ \.php$ {
    include fastcgi.conf;
    fastcgi_intercept_errors on;
    # By all means use a different server for the fcgi processes if you need to
    fastcgi_pass   127.0.0.1:9000;
  }

  location ~ /\.ht {
    deny  all;
  }
}
5 Answers

you must to be allow your 8080 port in firewall

If you're not installed ufw install it like

$ sudo apt install ufw

Then check the ufw status

$ sudo ufw status

Check your needed port is added into the list.

then use

 $ sudo ufw allow 8080 

then check the ufw status again. now it show you the allowed ipv4, ipv6 ports and connections. Restart your nginx server and check the port is working

$ sudo systemctl nginx restart
$ curl -I 'http://{youraddress}:8080'

Or hit the url in your browser.

If you are using GCP, AWS check the external firewall settings in your server console. like http_server allow 80. and edit this record to 80, 8080 and save it.

Caution: Don't do the internal firewall settings using ufw in GCP, AWS because already they have their own firewall settings for the engines.

Thank you

Your configuration are correct. I maybe add "server_name" to each server.

You should add logging to each virtual host, adding this to each server {} entry:

access_log  /var/log/nginx/server.intra-upstream.log upstream buffer=16k;
access_log  /var/log/nginx/server.intra-access.log combined buffer=16k;

You also need to add to your http {} entry to make upstream log works:

log_format upstream '$remote_addr - $remote_user [$time_local]  $request '
                    '$upstream_addr $upstream_cache_status $upstream_status $upstream_response_time';

You can check the requests logs at those files. Upstream log the transaction with PHP-FPM and access the HTTP request.

You could have some iptables block or other network limitation.

To check if your server are listening at port 8080, use this command as root:

bash# netstat -nap | grep -i nginx

This will show you something like this:

tcp        0      0 0.0.0.0:443             0.0.0.0:*               LISTEN      939/nginx: master p
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      939/nginx: master p
tcp        0      0 0.0.0.0:81              0.0.0.0:*               LISTEN      939/nginx: master p
tcp        0      0 0.0.0.0:82              0.0.0.0:*               LISTEN      939/nginx: master p
unix  3      [ ]         STREAM     CONNECTED     17513    939/nginx: master p
unix  3      [ ]         STREAM     CONNECTED     17512    939/nginx: master p
unix  3      [ ]         STREAM     CONNECTED     17518    939/nginx: master p
unix  3      [ ]         STREAM     CONNECTED     17519    939/nginx: master p

In my case, NGiNX are listening at port 80, 81, 82 and 443.

To check you iptables rules, type:

bash# iptables-save

I'll not put here the output because it may vary a lot, and you just need to check the default INPUT rule (at top of output), like this:

*filter
:INPUT DROP [67342:4090666]

In my case, INPUT are default DROP. If you see an ACCEPT there, it's ok, otherwise you need to check if there's any explicit rule accepting packets at the port 8080.

If you've INPUT as DROP, you can test if this are your problem opening port 8080 with this command:

bash# iptables -A INPUT -p tcp -m tcp --dport 8080 -j ACCEPT

I have tested your configuration, and it seems correct to me. Some things that comes to my mind that could cause the unexpected behavior you are experiencing:

  • As others have said, did you restart nginx after creating the last conf file? I know it does sound silly but i forgot to do that myself a lot of time.
  • Permission issues or wrong path for the root domain directive. Are you sure the www.domain2.com files directory exists and/or is readable by www-data / your nginx user?
  • Check your DNS. Did you remember to add the www.domain2.com and other domain to your hosts file (assuming you are working on your own box, of course)? Are your domains reachable via ping or telnet?
  • Try to validate your sintax, sudo nginx -t. Does it say something interesting?

as you said:

access and upstream show nothing

so the truth is elsewhere : look at your firewall. if you are using AWS, look at your VPC configuration (subnet, nacl, security group).

On nginx side, it seems fine, at least you must have a log access when pointing to index.php. If you want to see a log of your static files, you should comment the line

access_log off;

(only for testing, put it back from production).

I had the same exact problem and as described in a comment above the reason was that the firewall was blocking the port

Related