Join the community today
Register Now

Nginx Nginx conf file syntax error. I'm stumped

Discussion in 'Nginx, PHP-FPM & MariaDB MySQL' started by Andy, Oct 19, 2017.

  1. Andy

    Andy Active Member

    331
    47
    28
    Aug 6, 2014
    Ratings:
    +56
    Local Time:
    1:07 AM
    I have a very simple nginx.conf file that I tried to test but couldn't file out why nginx complained.
    Code:
    server {
          listen   80;
           server_name mfeapp.mydomain.com
    
           server_name 10.10.10.10;
           # note that these lines are originally from the "location /" block
           root   /usr/share/nginx/html;
           index index.php index.html index.htm;
     
           location / {
              try_files $uri $uri/ =404;
          }
          error_page 404 /404.html;
          error_page 500 502 503 504 /50x.html;
          location = /50x.html {
              root /usr/share/nginx/html;
          }
    
          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;
          }
     }
    When I tested the conf using the nginx command, it will report this
    Code:
    nginx -c /etc/nginx/conf.d/default.conf -t
    nginx: [emerg] "server" directive is not allowed here in /etc/nginx/conf.d/default.conf:1
    I'm stumped. Not sure what else to look at.
     
  2. wmtech

    wmtech New Member

    14
    7
    3
    Jul 22, 2017
    Ratings:
    +9
    Local Time:
    7:07 AM
    If this is your only configuration, it is missing a global http directive. "server" needs a "http" wrapped around it.
     
    • Like Like x 1
  3. Andy

    Andy Active Member

    331
    47
    28
    Aug 6, 2014
    Ratings:
    +56
    Local Time:
    1:07 AM
    After trying to get CMM to run on RH 7.3 was not successful, I tried to do it the usual way, installed nginx, php separately. I looked at many tutorials on how to get a simple nginx installation working and still couldn't figure out why my nginx does not work.

    @eva2000 would really appreciate if you can provide some hints, whenever you can.

    After yum install nginx, I have to conf files. I only edit the default.conf file

    Here is the content of /etc/nginx/nginx.conf
    Code:
    user  nginx;
    worker_processes  1;
    
    error_log  /var/log/nginx/error.log warn;
    pid        /var/run/nginx.pid;
    
    
    events {
        worker_connections  1024;
    }
    
    
    http {
        include       /etc/nginx/mime.types;
        default_type  application/octet-stream;
    
        log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                          '$status $body_bytes_sent "$http_referer" '
                          '"$http_user_agent" "$http_x_forwarded_for"';
    
        access_log  /var/log/nginx/access.log  main;
    
        sendfile        on;
        #tcp_nopush     on;
    
        keepalive_timeout  65;
    
        gzip  on;
    
        include /etc/nginx/conf.d/*.conf;
    }
    Here is content of /etc/nginx/conf.d/default.conf
    Code:
    server {
          listen 80;
           server_name mfeapp1v.domain.com 10.1.105.62;
    
           # note that these lines are originally from the "location /"
           # block
           root /usr/share/nginx/html;
           index index.php index.html index.htm;
    
           location / {
              try_files $uri $uri/ =404;
          }
          error_page 404 /404.html;
          error_page 500 502 503 504 /50x.html;
          location = /50x.html {
              root /usr/share/nginx/html;
          }
    
          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;
          }
     }
    Then I do a nginx config file test
    Code:
    nginx -t
    nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
    nginx: configuration file /etc/nginx/nginx.conf test is successful
    I make sure both nginx and php-fpm services are running
    Code:
    systemctl status php-fpm.service -l
    ● php-fpm.service - The PHP FastCGI Process Manager
       Loaded: loaded (/usr/lib/systemd/system/php-fpm.service; enabled; vendor preset: disabled)
       Active: active (running) since Wed 2017-10-18 10:21:41 EDT; 1 day 2h ago
     Main PID: 24625 (php-fpm)
       Status: "Processes active: 0, idle: 5, Requests: 0, slow: 0, Traffic: 0req/sec"
       CGroup: /system.slice/php-fpm.service
               ├─24625 php-fpm: master process (/etc/php-fpm.conf
               ├─24626 php-fpm: pool www
               ├─24627 php-fpm: pool www
               ├─24628 php-fpm: pool www
               ├─24629 php-fpm: pool www
               └─24630 php-fpm: pool www
    
    Oct 18 10:21:41 mfeapp1v.domain,com systemd[1]: Starting The PHP FastCGI Process Manager...
    Oct 18 10:21:41 mfeapp1v.domain.com systemd[1]: Started The PHP FastCGIProcess Manager.
    Code:
    systemctl status nginx.service -l
    ● nginx.service - nginx - high performance web server
       Loaded: loaded (/usr/lib/systemd/system/nginx.service; enabled; vendor preset: disabled)
       Active: active (running) since Thu 2017-10-19 12:21:16 EDT; 5min ago
         Docs: http://nginx.org/en/docs/
     Main PID: 451 (nginx)
       CGroup: /system.slice/nginx.service
               ├─451 nginx: master process /usr/sbin/nginx -c /etc/nginx/nginx.con
               └─452 nginx: worker process
    
    Oct 19 12:21:16 mfeapp1v.domain.com systemd[1]: Starting nginx - high performance web server...
    Oct 19 12:21:16 mfeapp1v.domain.com nginx[448]: nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
    Oct 19 12:21:16 mfeapp1v.domain.com nginx[448]: nginx: configuration file /etc/nginx/nginx.conf test is successful
    Oct 19 12:21:16 mfeapp1v.domain.com systemd[1]: Failed to read PID fromfile /var/run/nginx.pid: Invalid argument
    Oct 19 12:21:16 mfeapp1v.domain.com systemd[1]: Started nginx - high performance web server.
    When I go to the address 10.1.105.62, I got a blank page with this info
    Code:
    This site can’t be reached
    10.1.105.62 refused to connect.
    
    Try:
    
    [LIST]
    [*]Checking the connection
    [*]Checking the proxy and the firewall
    [/LIST]
    ERR_CONNECTION_REFUSED
    However, if I try to get the header of the site, this is what I get
    Code:
    ##curl -I 10.1.105.62
    HTTP/1.1 200 OK
    Server: nginx/1.13.6
    Date: Thu, 19 Oct 2017 16:29:48 GMT
    Content-Type: text/html
    Content-Length: 612
    Last-Modified: Tue, 10 Oct 2017 15:59:40 GMT
    Connection: keep-alive
    ETag: "59dcee6c-264"
    Accept-Ranges: bytes
    I was expecting the usual welcome Nginx page. What do I miss @eva2000 ?
     
  4. eva2000

    eva2000 Administrator Staff Member

    30,578
    6,854
    113
    May 24, 2014
    Brisbane, Australia
    Ratings:
    +10,275
    Local Time:
    4:07 PM
    Nginx 1.13.x
    MariaDB 5.5
    firewalld firewall is probably active and needs whitelisting http, https ports etc
     
    • Winner Winner x 1
  5. Andy

    Andy Active Member

    331
    47
    28
    Aug 6, 2014
    Ratings:
    +56
    Local Time:
    1:07 AM
    That's it. Stopping firewall allows me to see the nginx welcome page.
    Since this is not CMM, I have to google on how to open a port
    Code:
    sudo firewall-cmd --zone=public --add-port=80/tcp --permanent
    sudo firewall-cmd --reload
    Thanks George.
     
    • Like Like x 1