1

I have a server that runs nginx with varnish. I cannot run nginx and varnish at the same time as the they are both trying to listen on port 80.

nginx is configured to listen on port 8080

        upstream www {
        server 127.0.0.1:9001;
    }
    server {
        listen 8080 default_server;
        server_name server.com;
        root /var/www/html/;
    location /docs/index.php {
            fastcgi_pass www;
            fastcgi_split_path_info ^(.+\.php)(/.*)$;
            include fastcgi_params;
            fastcgi_param  SCRIPT_FILENAME $document_root$fastcgi_script_name;
            fastcgi_param  HTTPS off;
            fastcgi_param  SERVER_PORT 8888;
        }
}

Varnish then listens on port 80

NFILES=131072                                                                              
MEMLOCK=82000                                                                              
NPROCS="unlimited"                                                                         
RELOAD_VCL=1                                                                               
VARNISH_VCL_CONF=/etc/varnish/default.vcl                                                  
VARNISH_LISTEN_PORT=80                                                                     
VARNISH_ADMIN_LISTEN_ADDRESS=127.0.0.1                                                     
VARNISH_ADMIN_LISTEN_PORT=6082                                                             
VARNISH_SECRET_FILE=/etc/varnish/secret                                                    
VARNISH_MIN_THREADS=50                                                                     
VARNISH_MAX_THREADS=1000                                                                   
VARNISH_THREAD_TIMEOUT=120                                                                 
VARNISH_STORAGE_FILE=/var/lib/varnish/varnish_storage.bin                                  
VARNISH_STORAGE_SIZE=1G                                                                    
VARNISH_STORAGE="file,${VARNISH_STORAGE_FILE},${VARNISH_STORAGE_SIZE}"                     
VARNISH_TTL=120                                                                            
DAEMON_OPTS="-a ${VARNISH_LISTEN_ADDRESS}:${VARNISH_LISTEN_PORT} \                         
             -f ${VARNISH_VCL_CONF} \                                                      
             -T ${VARNISH_ADMIN_LISTEN_ADDRESS}:${VARNISH_ADMIN_LISTEN_PORT} \             
             -t ${VARNISH_TTL} \                                                           
             -w ${VARNISH_MIN_THREADS},${VARNISH_MAX_THREADS},${VARNISH_THREAD_TIMEOUT} \  
             -u varnish -g varnish \                                                       
             -S ${VARNISH_SECRET_FILE} \                                                   
             -s ${VARNISH_STORAGE}"

When I try and restart nginx now I get an error

service nginx restart
Stopping nginx:                                            [FAILED]
Starting nginx: nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx: [emerg] still could not bind()

If I stop varnish and run netstat I can see that nginx runs on port 8080 and 80

netstat -tulpn | grep nginx
tcp        0      0 0.0.0.0:80                  0.0.0.0:*                   LISTEN      95831/nginx
tcp        0      0 0.0.0.0:8080                0.0.0.0:*                   LISTEN      95831/nginx

Checking the nginx config directory, I cannot see anywhere that port is defined

grep -rnw '/etc/nginx/' -e '80'  


grep -rnw '/etc/nginx/' -e 'server'
/etc/nginx/nginx.conf.rpmnew:40:    server {
/etc/nginx/nginx.conf.rpmnew:46:        # Load configuration files for the default server block.
/etc/nginx/nginx.conf.rpmnew:52:        # redirect server error pages to the static page /40x.html
/etc/nginx/nginx.conf.rpmnew:58:        # redirect server error pages to the static page /50x.html
/etc/nginx/nginx.conf.rpmnew:70:        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
/etc/nginx/nginx.conf.rpmnew:88:# Settings for a TLS enabled server.
/etc/nginx/nginx.conf.rpmnew:90:#    server {
/etc/nginx/nginx.conf.rpmnew:96:#        ssl_certificate "/etc/pki/nginx/server.crt";
/etc/nginx/nginx.conf.rpmnew:97:#        ssl_certificate_key "/etc/pki/nginx/private/server.key";
/etc/nginx/nginx.conf.rpmnew:107:#        # Load configuration files for the default server block.
/etc/nginx/nginx.conf:40:#    server {
/etc/nginx/nginx.conf:46:#        # Load configuration files for the default server block.
/etc/nginx/nginx.conf:52:        # redirect server error pages to the static page /40x.html
/etc/nginx/nginx.conf:58:        # redirect server error pages to the static page /50x.html
/etc/nginx/nginx.conf:70:        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
/etc/nginx/nginx.conf:88:# Settings for a TLS enabled server.
/etc/nginx/nginx.conf:90:#    server {
/etc/nginx/nginx.conf:96:#        ssl_certificate "/etc/pki/nginx/server.crt";
/etc/nginx/nginx.conf:97:#        ssl_certificate_key "/etc/pki/nginx/private/server.key";
/etc/nginx/nginx.conf:107:#        # Load configuration files for the default server block.
/etc/nginx/conf.d/virtual.conf:5:#server {
/etc/nginx/conf.d/server.conf:2:    server 127.0.0.1:9001;
/etc/nginx/conf.d/server.conf:4:server {
/etc/nginx/conf.d/server.conf:170:server {
/etc/nginx/nginx.conf.default:35:    server {
/etc/nginx/nginx.conf.default:50:        # redirect server error pages to the static page /50x.html
/etc/nginx/nginx.conf.default:63:        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
/etc/nginx/nginx.conf.default:84:    #server {
/etc/nginx/nginx.conf.default:96:    # HTTPS server
/etc/nginx/nginx.conf.default:98:    #server {
Bridgewater
  • 31
  • 1
  • 2
  • 6
  • Can you please post the output of `grep -rnw '/etc/nginx/' -e 'server'` ? – Pothi Kalimuthu Jul 02 '18 at 10:40
  • You may have more than one `server` location (apart from what's mentioned in the question). When Nginx see a `server` without a `listen` directive, it starts to listen on port 80. – Pothi Kalimuthu Jul 02 '18 at 10:42
  • I see another server block at `/etc/nginx/conf.d/server.conf:170:server {`. Does this contain a listen directive? – Pothi Kalimuthu Jul 02 '18 at 10:49
  • How did you solve it please ? I face same problem and I'm desperate to find where this 80 port comes from ! – Kojo Jun 27 '20 at 11:51
  • If you are getting the same error as me. Then something is running on port 80. Use netstat -tulpn to see what is using that port. If it is nginx. Then run grep -rnw /etc/nginx/ -e '80' – Bridgewater Jun 28 '20 at 17:45

1 Answers1

3

Check that all the Server blocks have listen directives, otherwise nginx will default to 80 for that block.

Joris
  • 5,939
  • 1
  • 15
  • 13