Nginx ports (8080)

May 30, 2013 35.9k views
Hi, I'm configuring nginx and everything seems to work OK, except that it only "listens" to the default port (80). I'm struggling to configure it to listen to port 8080. I've tried with "listen 8080;" "listen 127.0.0.1:8080;" "listen localhost:8080;"... but all are ignored. I made those changes in the sites-enabled file (/etc/nginx/sites-available/mysite). Do I have to change something in nginx.conf or in other file? Thanks in advance for your help!
1 comment
16 Answers
(typo: where I say "sites-enabled file" I'm referring to "sites-available file")
Try editing the listen directive so it looks like this:

listen 8080;

Also make sure the file you're editing exists in the sites-enabled directory. Don't forget to restart/reload nginx after editing the config files: service nginx restart/reload
Thanks Kamal, I was already doing all that, without luck. As I've said, it works when I put:
listen 80; (it also works when I erase the listen line, because 80 is default)
But it doesn't work when I put:
listen 8080; (or listen 127.0.0.1:8080; or listen localhost:8080;)
Any ideas?

Make sure you have 8080, restart nginx and check the error logs.
Do you see any errors?
Are you running a firewall? If you want to connect externally to it, you may have to add a rule in to iptables.
The first virtual block you set up is a catch all. The succeeding blocks respond to their server_name directive. Make sure that you're placing it under a valid server block. A specific server block responds only to the EXACT server_name directive that you specify. All others which do not match any server block go to the first one. Also, check your firewall.
Kamal, I checked and there are some errors, but apparently all related to my CMS (Drupal), not to nginx.

Gavin, I haven't installed any firewall (at least not consciously).

Dave, I don't fully understand what you say (I'm very new to nginx and to sysadmin issues in general) but I think that could be the problem or at least related to that:
I have several domains in the same nginx server and therefore several "server declarations" in the same configuration file (all included in /etc/nginx/sites-available/mysite). When I put listen 8080; in the first server declaration (save and restart nginx) and go to that first domain, it redirects to the next domain in the nginx configuration file.

Simplifying, it's something like this:

server {
listen 8080;
server_name www.domain1.com;
....
}
server {
listen 80;
server_name www.domain2.com;
....
}

With this configuration, when I open www.domain1.com in my browser it redirects me to www.domain2.com. That could be related to what you were talking about? That gives more clues about what could be my problem?

Thanks again for all your quick responses!
@delvallelajas You have to create a default fallback virtualhost that nginx should serve if it gets a request for a Host/domain name it doesn't recognize.
Thanks again Kamal, but I would really appreciate if you could elaborate it a bit more (specific code of what do I have to add would be wonderful), because I don't know enough to translate your advice to code.

I've googled "default fallback virtualhost nginx" and reached to http://nginx.org/en/docs/http/server_names.html and http://wiki.nginx.org/NginxVirtualHostExample#A_Default_Catchall_Virtual_Host, but I don't know if that is what you are talking about and I don't know how to apply it to my case anyway.

I've tried adding a code included in that first link (talking about "catch-all servers"):
server {
listen 80 default_server;
server_name _;
return 444;
}

When I add this, I get Error 324 (net::ERR_EMPTY_RESPONSE):
@delvallelajas The virtualhost block you added is correct. You just have to replace "return 444;" with "root /var/www/default/public_html" where /var/www/default/public_html is the path to the directory that holds the files that should be served in case nginx receives a request for a domain name it can't recognize.
Well, probably I'm doing something wrong, because it doesn't work either.

If I put something like the following config, I get 403 forbidden:

server {
listen 80 default_server;
server_name _;
root /var/www/default/public_html; (changing that to my appropriate directory)
}
server {
listen 8080;
server_name www.domain1.com;
....
}
server {
listen 80;
server_name www.domain2.com;
....
}

And if i put something like the following config (listen 8080 instead of listen 80 in default_server), it happens the same as before without the default_server code (it redirects from domain1 to domain2):

server {
listen 8080 default_server;
server_name _;
root /var/www/default/public_html; (changing that to my appropriate directory)
}
server {
listen 8080;
server_name www.domain1.com;
....
}
server {
listen 80;
server_name www.domain2.com;
....
}

This is not indispensable for my project right now, so guess I'm going to give up on this. It seems too difficult for somebody with my almost inexistent knowledge of nginx. Thanks anyway for all your help.
@delvallelajas keep the configuration you posted first, restart nginx, reload your browser, and check the error files in /var/log/nginx. Paste the last few lines, the cause of the 403 Forbidden error should be there.
It says the following (I edited the paths and domains for security/privacy):

2013/06/02 13:22:48 [error] 9861#0: *6 directory index of "/home/path/whatever/" is forbidden, client: 87.221.25.37, server: _, request: "GET / HTTP/1.1", host: "www.domain.com"
2013/06/02 13:22:49 [error] 9861#0: *6 open() "/home/path/whatever/favicon.ico" failed (2: No such file or directory), client: 87.221.25.37, server: _, request: "GET /favicon.ico HTTP/1.1", host: "www.domain.com"

I thought that was the correct path, but maybe that's the problem. You talked about "/var/www/default/public_html", but I don't have a that directory (there is no "www" in "var").
My files are in "/home/path/whatever/domain1", "/home/path/whatever/domain2"... so I guess "/home/path/whatever/" is the "directory that holds the files that should be served in case nginx receives a request for a domain name it can't recognize". Please correct me if I didn't understood it correctly.

Maybe it's also relevant to mention that I'm using Drupal and my nginx config is the one described in this page: http://wiki.nginx.org/Drupal (it's the first result that appears when you search "drupal nginx" in google, so I guess it's a standard and well tested config)

What would you like to happen when someone vists www.domain1.com on port 80?

Hi kamaln7.
I config Nginx run on port 80 and httpd run on port 8080. But when i test my code on local only apache installed, it's ok. But when I up code on server, It auto add 8088 to all my link href, hix hixx. This make terible my system. It have error :(.

Folks, this is a banana slip ! the conf file is being generated dynamically and actually does not point to /etc/nginx/available-sites/default !!! Take a look at ps -ef | grep nginx and you will see the actual conf file !!!

Have another answer? Share your knowledge.