My Nginx server is not working

fgtr asked:

I have an Apache server on Ubunu 18.04 with a Drupal 8 site that I want to migrate to Nginx and PHP7.3-FPM.

This is the only site on the server. I do not know Nginx at all. The site works with Apache but it does not work with Nginx.

I deleted Apache with the following command :

sudo apt autoremove --purge apache2*

Here is the configuration I had on Apache :

<VirtualHost *:80>
   ServerAdmin [email protected]
   ServerName s1biose.com
   ServerAlias www.s1biose.com
   Protocols h2 http/1.1
   DocumentRoot /var/www/www-s1biose-com/web/

   <Directory /var/www/www-s1biose-com/web>
      Options +Includes -Indexes +FollowSymLinks
      AllowOverride All
      Require all granted
   </Directory>

   <FilesMatch \.php$>
      SetHandler "proxy:unix:/var/run/php/php7.3-fpm.sock|fcgi://localhost/"
   </FilesMatch>

   ErrorLog ${APACHE_LOG_DIR}/error.log
   CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

Here’s how I installed Nginx :

sudo apt install nginx
sudo ufw allow in "Nginx HTTP"

I am testing the IP address of my server and the Nginx page is displayed.

sudo unlink /etc/nginx/sites-enabled/default

Here is my Nginx configuration :

sudo nano /etc/nginx/sites-available/www-s1biose-com

server {
    listen 80;
    listen [::]:80;
    server_name s1biose.com www.s1biose.com;

    root   /var/www/www-s1biose-com/web;
    index  index.html index.php;

    location / {
        try_files $uri $uri/ =404;
    }
    location ~ \.php$ {
            include snippets/fastcgi-php.conf;
            include fastcgi_params;
            fastcgi_pass unix:/run/php/php7.3-fpm.sock;
            fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    }
}

I create a symbolic link of my new configuration :

sudo ln -s /etc/nginx/sites-available/www.s1biose.com /etc/nginx/sites-enabled/

I test my configuration :

sudo nginx -t

This message is displayed :

nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

If I enter the url s1biose.com of my site, I have the following message :

enter image description here

When I go to the IP address of the site, I have this message :

404 Not Found nginx/1.14.0 (Ubuntu)

sudo ss -tnlp

State            Recv-Q            Send-Q                             Local Address:Port                       Peer Address:Port                                                                                                                                                        
LISTEN           0                 80                                     127.0.0.1:3306                            0.0.0.0:*                users:(("mysqld",pid=5833,fd=20))                                                                                                          
LISTEN           0                 128                                      0.0.0.0:80                              0.0.0.0:*                users:(("nginx",pid=17289,fd=6),("nginx",pid=17281,fd=6),("nginx",pid=17280,fd=6))                                                         
LISTEN           0                 128                                127.0.0.53%lo:53                              0.0.0.0:*                users:(("systemd-resolve",pid=702,fd=13))                                                                                                  
LISTEN           0                 128                                      0.0.0.0:22                              0.0.0.0:*                users:(("sshd",pid=903,fd=3))                                                                                                              
LISTEN           0                 100                                      0.0.0.0:25                              0.0.0.0:*                users:(("smtpd",pid=17394,fd=6),("master",pid=1432,fd=13))                                                                                 
LISTEN           0                 128                                 51.68.41.141:2812                            0.0.0.0:*                users:(("monit",pid=897,fd=6))                                                                                                             
LISTEN           0                 50                            [::ffff:127.0.0.1]:7983                                  *:*                users:(("java",pid=1062,fd=48))                                                                                                            
LISTEN           0                 128                                         [::]:80                                 [::]:*                users:(("nginx",pid=17289,fd=7),("nginx",pid=17281,fd=7),("nginx",pid=17280,fd=7))                                                         
LISTEN           0                 128                                            *:8080                                  *:*                users:(("node /var/www/w",pid=4281,fd=11))                                                                                                 
LISTEN           0                 128                                         [::]:22                                 [::]:*                users:(("sshd",pid=903,fd=4))                                                                                                              
LISTEN           0                 50                                             *:8983                                  *:*                users:(("java",pid=1062,fd=125))                                                                                                           
LISTEN           0                 100                                         [::]:25                                 [::]:*                users:(("smtpd",pid=17394,fd=7),("master",pid=1432,fd=14))

My answer:


Your Drupal 8 installation is redirecting homepage traffic to /fr, which then returns the nginx 404.

See the headers:

$ curl -I http://s1biose.com/
HTTP/1.1 301 Moved Permanently
Server: nginx/1.14.0 (Ubuntu)
Content-Type: text/html; charset=UTF-8
Connection: keep-alive
Date: Wed, 27 Feb 2019 06:11:22 GMT
Location: http://s1biose.com/fr
X-Drupal-Route-Normalizer: 1
X-UA-Compatible: IE=edge
Content-language: fr
X-Content-Type-Options: nosniff
X-Frame-Options: SAMEORIGIN
X-Generator: Drupal 8 (https://www.drupal.org)
X-Drupal-Cache: HIT

You should do the following, in order:

  1. Fix the server’s clock. It is obviously several hours wrong.
  2. Clear Drupal’s cache.
  3. Check what content your Drupal homepage is returning.

View the full question and any other answers on Server Fault.

Creative Commons License
This work is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License.