0

On my Nginx webserver i have several virtual hosts like this: - api.example.com - www.example.com - cv.example.com

But when i am visiting www.example.com/example and this is not an valid path its giving me 404 page of my api.example.com. But why ?

This is my current nginx configuration of www.example.com :

server {
    listen 443 ssl;
    listen [::]:443 ssl;

    access_log /var/log/nginx/www.example.com-access.log timed;
    error_log /var/log/nginx/www.example.com-error.log;
    root /var/www/wwwexamplecom/html/_site;
    server_name example.com www.example.com;
    add_header X-Content-Type-Options nosniff;
    add_header X-XSS-Protection "1; mode=block";

    include snippets/ssl-example.com.conf;
    include snippets/ssl-params.conf;

    location / {
        index index.html index.php;
        try_files $uri $uri/ /index.php?q=$uri&$args;
    }

    location ~ \.php$ {
        fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;
        fastcgi_param  SCRIPT_FILENAME /var/www/example/html/public/index.php;
        include fastcgi_params;
    }

    location ~ /.well-known {
                allow all;
        }
}

This is the configuration of my api.example.com :

server {
    listen 443 ssl;
    listen [::]:443 ssl;

    access_log /var/log/nginx/api.example.com-access.log timed;
    error_log /var/log/nginx/api.example.com-error.log;
    root /var/www/apiexamplecom/html/public;
    server_name api.example.com;

    include snippets/ssl-api.example.com.conf;
    include snippets/ssl-params.conf;

    location / {
        index index.html index.php;
        try_files $uri $uri/ /index.php?q=$uri&$args;
    }

    location ~ \.php$ {
        fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;
        fastcgi_param  SCRIPT_FILENAME /var/www/apiexamplecom/html/public/index.php;
        include fastcgi_params;
    }

    location ~ /.well-known {
                allow all;
        }
}

I think self its in the / location part but i am not really how i can fix this issue. This is also happening on other virtualhost.

Noob
  • 732
  • 8
  • 30

1 Answers1

0

You have to explicitly let nginx know that you want to throw a 404 error if it can't find anything in the location block. Otherwise it will try matching your 'default' server block (which in this case seems to be the api.example.com one because you haven't specified the default). It can't find anything in that server either, THEN it tries the 404.

To explicitly tell nginx to throw a 404 if it can't find anything in your location block, add =404 to the end of your try_files line. This means that if it can't find any files for $uri, $uri/ or any php files, then throw the 404 without trying anything else.

try_files $uri $uri/ /index.php?q=$uri&$args =404;
sharif9876
  • 680
  • 6
  • 19