I am having a problem with my nginx (and php-fpm) virtual host configuration. The problem is all requests (except static files) return HTTP 500 (Internal Server Error)
Here is my nginx vh code
server {
listen 80;
root /var/www/somesite/public;
index index.php index.html index.htm;
server_name somesite.com;
location / {
try_files $uri $uri/ /index.php?$query_string;
}
location ~ \.php$ {
try_files $uri /index.php =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
However when I serve using php's inbuilt development server everything works fine. FYI it's a laravel app
Thanks in advance
Related
I am having an issue with WP pretty permalink and my nginx conf.
Homepage works good
Post = 404
WP-Admin = 404
Can you help me to resolve it?
Relevant nginx configuration (excludes ssl, gzip, headers, etc.):
server {
server_name sampledomain.com;
listen 443 ssl http2;
root /var/www/sampledomain/;
index index.php;
...
location ~ \.php$ {
try_files $uri $uri/ /index.php?$args;
include fastcgi_params;
fastcgi_pass unix:/run/php/php7.2-fpm.sock;
fastcgi_split_path_info ^(.+\.php)(.*)$;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
}
Based on your config here, I think you need to add another location block at or before line 39.
For nginx, a web server aimed at high concurrency, high performance
and low memory usage, add the following location block within the
server block:
location / {
try_files $uri $uri/ /index.php?$args;
}
via WordPress Docs
Example:
server {
server_name sampledomain.com;
listen 443 ssl http2;
root /var/www/sampledomain/;
index index.php;
...
location / {
try_files $uri $uri/ /index.php?$args;
}
location ~ \.php$ {
try_files $uri $uri/ /index.php?$args;
include fastcgi_params;
fastcgi_pass unix:/run/php/php7.2-fpm.sock;
fastcgi_split_path_info ^(.+\.php)(.*)$;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
}
I have a React frontend and a Symfony backend I'm trying to serve on the same domain. The React frontend needs to serve assets if they exist otherwise fallback to serve index.html.
I'd like to serve the php Symfony app when /api is in the request uri. Similar to the React app, I need all requests to go to the index.php file.
The frontend is being served correctly but not the api. I get a 404 from nginx when i hit /api in the browser.
I feel like i'm close but for some reason nginx doesn't have the correct $document_root. I'm adding a header(X-script) to test what the variables are and I'm getting the following:
X-script: /usr/share/nginx/html/index.php
Here's my nginx config.
server {
listen 80 default_server;
index index.html index.htm;
access_log /var/log/nginx/my-site.com.log;
error_log /var/log/nginx/my-site.com-error.log error;
charset utf-8;
location /api {
root /var/www/my-site.com/backend;
try_files $uri $uri/ /index.php$is_args$args;
}
location / {
root /var/www/my-site.com/frontend;
try_files $uri /index.html;
}
location ~* \.php$ {
add_header X-script "$document_root$fastcgi_script_name" always;
try_files $fastcgi_script_name =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param HTTPS off;
}
}
Any help would be much appreciated.
The web root of a Symfony 4 project must include the public subfolder. I am not using NGINX but I think this is the correct configuration:
location /api {
root /var/www/my-site.com/backend/public;
In the following vhost, the most important changes I made are :
commented out index directive in server block : it is handled directly in locations blocks
added a slash after location /api/ and remove unnecessary $uri/ in the same api location block
moved the php_fpm logic to index.php location block : you want all requests to be passed to front controller in Symfony app
For the same reason, moved the 404 logic to a general php block, which will handle any other php file request
server {
listen 80 default_server;
access_log /var/log/nginx/my-site.com.log;
error_log /var/log/nginx/my-site.com-error.log error;
charset utf-8;
location /api/ {
root /var/www/my-site.com/backend;
try_files $uri /index.php$is_args$args;
}
location / {
root /var/www/my-site.com/frontend;
try_files $uri /index.html;
}
location ~ ^/index\.php(/|$) {
fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param DOCUMENT_ROOT $realpath_root;
fastcgi_param HTTPS off;
internal;
}
# return 404 for all other php files not matching the front controller
# this prevents access to other php files you don't want to be accessible.
location ~ \.php$ {
return 404;
}
}
Last, I bet you'll have to add symfony public folder into api location block root directive.
This vhost was tested fine on my localhost with following tree.
api_test
- backend
- index.php
- frontend
- index.html
I can successfully access to
backend/index.php from api_test.dv/api
frontend/index.html from api_test.dv/
Kojos answer is excellent, but to make it completely functional a root directive needs to be added under server or an error message “primary script unknown” will be observed. This is almost always related to a wrongly set SCRIPT_FILENAME in the nginx fastcgi_param directive.
# Nginx configuration
server {
listen 80 default_server;
listen [::]:80 default_server;
server_name ${NGINX_HOST};
root /var/www/html/backend/public;
access_log /var/log/nginx/access_log.log;
error_log /var/log/nginx/error.log error;
charset utf-8;
location / {
root /var/www/html/frontend/build;
try_files $uri /index.html;
}
location /api {
alias /var/www/html/backend/public;
try_files $uri /index.php$is_args$args;
}
location ~ ^/index\.php(/|$) {
fastcgi_pass php-fpm:9000;
fastcgi_split_path_info ^(.+\.php)(/.*)$;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
fastcgi_param DOCUMENT_ROOT $realpath_root;
internal;
}
# return 404 for all other php files not matching the front controller
# this prevents access to other php files you don't want to be accessible.
location ~ \.php$ {
return 404;
}
}
I have two apps. One is simple php file and the other is a zend framework app. I am using Nginx. It is working for simple php app which is accessible via example.com/.
For the ZF3 app, nginx is working only for parent route and none of the child routes are working. The parent route is accessible via example.com/products. I want nginx to handle route actions such as example.com/products/add, example.com/products/view which are already defined in module config.
nginx config
server {
listen *:80;
server_name example.com;
index index.html index.htm index.php;
access_log /var/log/nginx/simpleapp.access.log combined;
error_log /var/log/nginx/simpleapp.error.log;
root /var/www/simpleapp;
location ~ \.php$ {
fastcgi_pass unix:/var/run/php/php7.1-fpm.sock;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
location ^~ /products {
alias /var/www/ecom/public;
try_files $uri $uri/ /index.php$is_args$args;
location ~ \.php$ {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php/php7.1-fpm.sock;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $request_filename;
}
}
}
nginx log file
FastCGI sent in stderr: "Primary script unknown" while reading response header from upstream
What works:
example.com/
example.com/products
What doesn't work:
example.com/products/add
example.com/products/view
example.com/products/categories
Do you really need two different declarations for very same php processing? I would stick with one and only differentiate the root using alias on /product for example:
server {
listen *:80;
server_name example.com;
index index.html index.htm index.php;
access_log /var/log/nginx/simpleapp.access.log combined;
error_log /var/log/nginx/simpleapp.error.log;
root /var/www/simpleapp;
location / {
try_files $uri $uri/ /index.php$is_args$args;
}
location /products {
alias /var/www/ecom/public;
}
location ~ \.php$ {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_index index.php;
fastcgi_pass unix:/var/run/php/php7.1-fpm.sock;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
}
I have a Symfony site at /var/www/mysite/symfony and a WordPress blog at /var/www/mysite/wordpress. How can I direct mysite.com/blog to WordPress and all other requests to Symfony? Here's what I've tried so far:
server {
server_name mysite.com;
root /var/www/mysite/symfony/web;
location / {
try_files $uri /app.php$is_args$args;
}
location ~ ^/(app|app_dev|config)\.php(/|$) {
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $request_filename;
fastcgi_index index.php;
fastcgi_pass 127.0.0.1:9000;
}
location ~ ^/blog {
root /var/www/mysite/wordpress;
try_files $uri $uri/ /blog/index.php?$args;
location ~ \.php$ {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $request_filename;
fastcgi_index index.php;
fastcgi_pass 127.0.0.1:9000;
}
}
access_log /var/log/nginx/mysite-access.log;
error_log /var/log/nginx/mysite-error.log;
}
With this configuration I get a 'File not found.' error when I visit mysite.com/blog. My nginx log file shows FastCGI sent in stderr: "Primary script unknown" while reading response header from upstream. I know it's not an issue with my php-fpm set up as Symfony runs fine at my site root.
Where am I going wrong?
I found a specific rule for installing wordpress in a subfolder:
# Replace all occurrences of [subfolder] with the folder where your WordPress install is located
# The WordPress rewrite
location /[subfolder] {
try_files $uri $uri/ /[subfolder]/index.php?$args;
}
# The default MODX rewrite</h1>
location / {
try_files $uri $uri/ #modx-rewrite;
}
Original post here: Web Rules: Nginx Rewrites, Redirects and other Cloud Configs | MODX Cloud
I've just installed nginx 1.0.8 and php-fpm and for the last 30 minutes I'm trying to rewrite the URL for Wordpress.
Here is what the Wordpress URL should look like:
http://localhost/website/blog/2011/10/sample-post/
I've looked at this tutorial: http://wiki.nginx.org/WordPress + many other on the web but every time I receive an 404 error (sometimes 403).
Here is what I did in my configuration file:
location /website/blog {
try_files $uri $uri/ /website/blog/index.php;
}
location ~ \.php$ {
root html;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_intercept_errors on;
fastcgi_split_path_info ^(/website/blog)(/.*)$;
include fastcgi_params;
error_page 404 /404.html;
}
With this configuration I'm receiving "403 forbidden" status.
What am I missing?
Could be the permission of the root site folder
This is an example that i use to wordpress
server {
listen 80;
server_name www.mysite.com mysite.com;
root /srv/www/mysite.com/public_html;
location / {
index index.html index.htm index.php;
try_files $uri $uri/ /index.php?$args;
}
rewrite /wp-admin$ $scheme://$host$uri/ permanent;
include /srv/www/mysite.com/public_html/*.conf;
location ~ \.php$ {
try_files $uri =404;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
Did you try restarting nginx after you have saved your config?
Also, check out my nginx/WordPress setup guide here:
http://themesforge.com/featured/high-performance-wordpress-part-3/