I have a nginx server and add a laravel project in /var/www/main-site and address site.com i added a wordpress project to /var/www/blog. i want to show blog with this address site.com/blog. this my nginx config
listen 80;
server_name site.com www.site.com;
root /var/www/main-site/public;
add_header X-Frame-Options "SAMEORIGIN";
add_header X-XSS-Protection "1; mode=block";
add_header X-Content-Type-Options "nosniff";
index index.html index.htm index.php;
error_log /var/log/nginx/error.log;
charset utf-8;
location /blog {
alias /var/www/blog;
try_files $uri $uri/ /index.php?$query_string;
location ~ \.php$ {
fastcgi_pass unix:/var/run/php/php8.0-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
include fastcgi_params;
}
}
location / {
try_files $uri $uri/ /index.php?$query_string;
}
location = /favicon.ico { access_log off; log_not_found off; }
location = /robots.txt { access_log off; log_not_found off; }
error_page 404 /index.php;
location ~ \.php$ {
fastcgi_pass unix:/var/run/php/php8.0-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
include fastcgi_params;
}
location ~ /\.(?!well-known).* {
deny all;
}
}
when i call site.com, it works and shows laravel project. but when i call site.com/blog, I see File not found with 404 status. and in nginx log shows this error
2024/04/30 22:12:16 [error] 3577550#3577550: *7 FastCGI sent in stderr: "Primary script unknown" while reading response header from upstream, client: xxx.xxx.xx.xx, server: site.com, request: "GET /blog/ HTTP/1.1", upstream: "fastcgi://unix:/var/run/php/php8.0-fpm.sock:", host: "site.com"
Try:
location ^~ /blog {
root /var/www;
try_files $uri $uri/ /blog/index.php?$query_string;
...
}
The location
statement needs the ^~
operator, otherwise /blog/index.php
will be processed by the wrong location ~ \.php$
block.
You should use root
instead of alias
- where you can.
The final parameter of the try_files
statement is the action taken when the file is not found. Did you mean to divert to your other application at /index.php
, or stay with /blog/index.php
?