Question

404 On CentOS8 NGINX

I have setup a latest centos 8 on the server with nginx

and i am having a multiple projects will be running at server

For example :

var/www/html/project1

var/www/html/project2

var/www/html/project3

I have configured my NGINX.conf file as below

http {
    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';
    log_format scripts '$document_root$fastcgi_script_name > $request';

    access_log  /var/log/nginx/access.log  main;
    access_log /var/log/nginx/scripts.log scripts;

    sendfile            on;
    tcp_nopush          on;
    tcp_nodelay         on;
    keepalive_timeout   65;
    types_hash_max_size 2048;

    include             /etc/nginx/mime.types;
    default_type        application/octet-stream;

    # Load modular configuration files from the /etc/nginx/conf.d directory.
    # See http://nginx.org/en/docs/ngx_core_module.html#include
    # for more information.
    include /etc/nginx/conf.d/*.conf;

    server {
        listen       80 default_server;
        listen       [::]:80 default_server;
        server_name  _;
        root         /var/www/html;

        # Load configuration files for the default server block.
        include /etc/nginx/default.d/*.conf;
        index index.php index.html index.htm;

        location / {
            try_files $uri /index.html index.php;
        }

        location ~ \.php$ {
                try_files $uri /index.php =404;
                fastcgi_pass unix:/var/run/php-fpm/www.sock;
                fastcgi_index index.php;
                fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
                include fastcgi_params;
        }

        error_page 404 /404.html;
            location = /40x.html {
        }

        error_page 500 502 503 504 /50x.html;
            location = /50x.html {
        }
    }
}

and server is running on the ipaddress so in server name i have just put _ as server name which will consider it localhost or ip.

But when i am running the projects

http://IPADDRESS/project1

http://IPADDRESS/project2

I am getting 404 Error and when i configured the script log. i found it going wrong as below

/var/log/nginx/scripts.log

/var/www/html/project1/public/404.html > GET /project1/ HTTP/1.1
/var/www/html/project2/public/404.html > GET /project2/ HTTP/1.1

Can please guide me where i am going wrong on NGINX configuration ?


Submit an answer

This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

Sign In or Sign Up to Answer

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.

Want to learn more? Join the DigitalOcean Community!

Join our DigitalOcean community of over a million developers for free! Get help and share knowledge in Q&A, subscribe to topics of interest, and get courses and tools that will help you grow as a developer and scale your project or business.

Hello there,

In addition to what has already been mentioned, I wanted to point out that CentOS Linux 8 has reached End Of Life (EOL) on December 31st, 2021.

I would strongly recommend planning either migration to another OS like Debian or Ubuntu, or upgrading to CentOS Stream.

I personally believe that the safest way to do this is to follow these steps here:

  • Spin up a new Droplet with the desired OS
  • Install the necessary services (webserver, sql server, etc.)
  • After that using rsync for example copy over your files and migrate your databases
  • Then test your website and server fully to make sure that everything is working as expected
  • Finally, change your DNS to point to the new Droplet

Regards, Alex

Hi @kashyapwaytoweb,

The Nginx.conf file is not really meant to be used that way so it’s possible this is comming from exactly that usage.

Can you please try and create the file config file in the folder /etc/nginx/sites-available/ In there you should be able to keep the same configuration. What I mean is, you’ll still be able to keep the following

    server {
        listen 80;
        listen [::]:80;
        server_name  _;
        root         /var/www/html;

        # Load configuration files for the default server block.
        include /etc/nginx/default.d/*.conf;
        index index.php index.html index.htm;

        location / {
            try_files $uri /index.html index.php;
        }

        location ~ \.php$ {
                try_files $uri /index.php =404;
                fastcgi_pass unix:/var/run/php-fpm/www.sock;
                fastcgi_index index.php;
                fastcgi_param SCRIPT_FILENAME 
                $document_root$fastcgi_script_name;
                include fastcgi_params;
        }

        error_page 404 /404.html;
            location = /40x.html {
        }

        error_page 500 502 503 504 /50x.html;
            location = /50x.html {
        }
    }

Save it and try again to load the applications.

If this doesn’t work, please try adding another location to your Nginx block for each app like so :

        location /project1 {
            try_files $uri /index.html index.php;
        }

Another thing to have in mind is to make sure you have an index.html or index.php file. If you are using any other type of file as an initial one, add it to the above line.

Don’t forget to add the symlink to the site-enabled folder after you are done

sudo ln -s /etc/nginx/sites-available/nameOfFIle /etc/nginx/sites-enabled/

Regards, KFSys