Question

nginx syntax error - listen directive not allowed

Posted January 1, 2021 4.3k views
NginxWordPressDigitalOceanLEMPLet's Encrypt

Hello,

I have been following through a few different articles, but am now getting an error.

"listen" directive not allowed here...

listen [::]:443 ssl; # managed by certbot

After I set up certbot it was fine, so I’m not sure what else I have done to affect it.

The only change I have made to the file is adding WordPress bits as below.

    location = /favicon.ico { log_not_found off; access_log off; }
    location = /robots.txt { log_not_found off; access_log off; allow all; }
    location ~* \.(css|gif|ico|jpeg|jpg|js|png)$ {
        expires max;
        log_not_found off;

And…

    location / {
        #try_files $uri $uri/ =404;
        try_files $uri $uri/ /index.php$is_args$args;

Any help is much appreciated.

edited by bobbyiliev

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

×
Submit an Answer
2 answers

Hi there,

Can you run the following command to test your Nginx config syntax:

sudo nginx -t

And then share the output here.

Also, can you share your whole Nginx server block here as well?

Regards,
Bobby

  • Hello @bobbyiliev

    nginx: [emerg] "listen" directive is not allowed here in /etc/nginx/sites-enabled/domain.com:31
    nginx: configuration file /etc/nginx/nginx.conf test failed
    
    
    server {
    
            root /var/www/domain.com/html;
            index index.html index.htm index.nginx-debian.html;
    
            server_name domain.com www.domain.com;
    
            location / {
    #               try_files $uri $uri/ =404;
                    try_files $uri $uri/ /index.php$is_args$args;
    
            location = /favicon.ico { log_not_found off; access_log off; }
            location = /robots.txt { log_not_found off; access_log off; allow all; }
            location ~* \.(css|gif|ico|jpeg|jpg|js|png)$ {
                    expires max;
                    log_not_found off;
    
            }
    
            location ~ \.php$ {
                    include snippets/fastcgi-php.conf;
                    fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
    
            }
    
    #       Location ~ /\.ht {
    #               deny all;
    #       }
    
        listen [::]:443 ssl; # managed by Certbot
        listen 443 ssl; # managed by Certbot
        ssl_certificate /etc/letsencrypt/live/domain2.com/fullchain.pem; # managed by Certbot
        ssl_certificate_key /etc/letsencrypt/live/domain2.com/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
    
    }
    server {
        if ($host = www.domain.com) {
            return 301 https://$host$request_uri;
        } # managed by Certbot
    
    
        if ($host = domain.com) {
            return 301 https://$host$request_uri;
        } # managed by Certbot
    
            listen 80;
            listen [::]:80;
    
            server_name domain.com www.domain.com;
        return 404; # managed by Certbot
    
    }
    
    
    • Hi there,

      The problem is with the first location definition here:

              location / {
      #               try_files $uri $uri/ =404;
                      try_files $uri $uri/ /index.php$is_args$args;
              }
      

      It is missing the closing }, once you add it, the problem will be fixed.

      Regards,
      Bobby

      • Thanks @bobbyiliev that sorted it.

        Also in there is a bit commented out because it causes an error, any ideas why? Not commented in snip below.

        nginx: [emerg] unknown directive "Location" in /etc/nginx/sites-enabled/domain.com:27
        nginx: configuration file /etc/nginx/nginx.conf test failed
        
        
        server {
        
                root /var/www/domain.com/html;
                index index.html index.htm index.nginx-debian.html;
        
                server_name Domain.com www.domain.com;
        
                location / {
        #               try_files $uri $uri/ =404;
                        try_files $uri $uri/ /index.php$is_args$args;
                }
        
                location = /favicon.ico { log_not_found off; access_log off; }
                location = /robots.txt { log_not_found off; access_log off; allow all; }
                location ~* \.(css|gif|ico|jpeg|jpg|js|png)$ {
                        expires max;
                        log_not_found off;
        
                }
        
                location ~ \.php$ {
                        include snippets/fastcgi-php.conf;
                        fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
        
                }
        
                Location ~ /\.ht {
                        deny all;
                }
        
        
        
        
Show answer This answer has been marked as resolved by patrickb.