Question

404 On CentOS8 NGINX

Posted February 11, 2021 74 views
NginxCentOS 8

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 ?

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.

×
1 answer

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

Submit an Answer