I have an nginx config for my server as follow, I want
when I request service/dev/sync/sync
it will route to /home/work/app-web/src/api/dev/sync.php/sync
match the php rule with $fastcgi_script_name is /dev/sync.php and $fastcgi_path_info is sync
this rule only need for ~ /service/([a-zA-Z]*/[a-zA-Z]*)(/[a-zA-Z]*)$.
But when I request /service/dev/sync/sync, it will return 404 with error
*173 open() "/home/work/nginx/html/dev/sync.php/sync" failed (2: No such file or directory), client: 172.18.17.90, server: localhost, request: "POST /service/dev/sync/sync HTTP/1.1"
/home/work/nginx/html is the default root for my nginx server. the root config is not
work at all.
what's the problem with this config?
server {
set $work_dir /home/work/app-web;
listen 80;
server_name localhost;
location ~ /service/([a-zA-Z]*/[a-zA-Z]*)(/[a-zA-Z]*)$ {
root $work_dir/src/api;
set $file $1;
set $action $2;
try_files $uri $uri/ /$file.php$action$is_args$query_string;
location ~ \.php {
fastcgi_split_path_info ^(.+\.php)/(.*)$;
fastcgi_pass unix:/home/work/app-web/php/var/run/app-web.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $work_dir/src/api$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info;
fastcgi_param UNIQUE_ID $request_id;
include fastcgi_params;
}
}
}
Root config works fine.
Your problem is that there is an internal redirection to /dev/sync.php/sync?, and the new inner-url does not match the regexp-location
You can activate the debug with
error_log /var/log/nginx/debug.log debug;
The logs look like this :
*1 trying to use file: "/dev/sync.php/sync" "/home/work/app-web/src/api/dev/sync.php/sync"
*1 internal redirect: "/dev/sync.php/sync?"
*1 rewrite phase: 1
*1 http script value: "/home/work/app-web"
*1 http script set $work_dir
*1 test location: ~ "/service/([a-zA-Z]*/[a-zA-Z]*)(/[a-zA-Z]*)$"
*1 using configuration ""
The new URL is /dev/sync.php/sync?, and this url matches with no location, because the regexp /service/([a-zA-Z]/[a-zA-Z])(/[a-zA-Z]*)$ is not intended for the inner-URL.
I suggest you another configuration :
server {
set $work_dir /home/work/app-web;
listen 80;
server_name localhost;
location ~ /service/([a-zA-Z]*/[a-zA-Z]*)(/[a-zA-Z]*)$ {
root $work_dir/src/api/;
set $file $1;
set $action $2;
rewrite .* /src/api/$file.php$action last;
}
location ~ \.php {
internal;
root $work_dir;
fastcgi_split_path_info ^(.+\.php)/(.*)$;
fastcgi_pass unix:/home/work/app-web/php/var/run/app-web.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root/$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info;
fastcgi_param UNIQUE_ID $request_id;
include fastcgi_params;
}
error_log /var/log/nginx/test-error.log debug;
}
Note the internal; directive
Related
Im learning NGINX, so any help is really appreciated.
I have the frontend of a website running as the root of mysite.com, and now I want to run wordpress from mysite.com/blog.
My file structure is:
/srv/mysite/frontend
/srv/mysite/wordpress
this is the error i've been getting from the nginx logs
2020/03/29 00:09:03 [error] 23049#23049: *39 FastCGI sent in stderr: "Primary script unknown" while reading response header from upstream, client: XXXXXXX, server: www.mysite.com, request: "GET /api HTTP/1.1", upstream: "fastcgi://unix:/run/php/php7.2-fpm.sock:", host: "mysite.com"
and this is my nginx config file so far
listen 80 default_server;
server_name www.mysite.com mysite.com;
charset utf-8;
location ^~ /blog {
root /srv/mysite/wordpress;
}
location ~ \.php$ {
fastcgi_pass unix:/run/php/php7.2-fpm.sock;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param SCRIPT_NAME $fastcgi_script_name;
}
location / {
root /srv/mysite/frontend/dist;
try_files $uri /index.html;
}
}
This error message shows either the wrong path of the sock file or the permissions. Make sure the php sock file exists in the path /run/php/php7.2-fpm.sock and change the permissions of the file.
For Debian
chown -R wwww-data:www-data /run/php/php7.2-fpm.sock
For Rhel
chown -R nginx:nginx /run/php/php7.2-fpm.sock
Also, you can try this config.
location /blog {
root /srv/mysite/wordpress;
}
location ~ \.php$ {
try_files $uri /index.php =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/run/php/php7.2-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
In the browser, I get "File not found". Server log and configuration are provided. I'm really not sure what to do at this point.
The Error:
2018/04/24 14:00:46 [error] 28717#28717: *9 FastCGI sent in stderr:
"Primary script unknown" while reading response header from upstream,
client: 127.0.0.1, server: localhost, request: "GET / HTTP/1.1",
upstream: "fastcgi://127.0.0.1:9000", host: "wasob.localhost"
wasob.localhost
server {
#Nginx should listen on port 80 for requests to yoursite.com
listen 80;
server_name wasob.localhost;
#Create access and error logs in /var/log/nginx
access_log /var/log/nginx/default.access_log;
error_log /var/log/nginx/wasob.error_log error;
root /var/www/html/wasob/;
#The homepage of your website is a file called index.php
index index.php;
location / {
try_files $uri $uri/ index.php
}
#Specifies that Nginx is looking for .php files
location ~ [^/]\.php(/|$) {
fastcgi_split_path_info ^(.+?\.php)(/.*)$;
#if (!-f $document_root$fastcgi_script_name) {
# return 404;
#}
fastcgi_index index.php;
# SCRIPT_FILENAME parameter is used for PHP FPM determining
# the script name. If it is not set in fastcgi_params file,
# i.e. /etc/nginx/fastcgi_params or in the parent contexts,
# please comment off following line:
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
# Mitigate https://httpoxy.org/ vulnerabilities
fastcgi_param HTTP_PROXY "";
# include the fastcgi_param setting
include fastcgi_params;
fastcgi_pass 127.0.0.1:9000;
}
}
The error and config are available at https://gist.github.com/skinuxgeek/4d4f86490f87805d1781782670551db9
Created a virtualhost for symfony application in local system
Here is the nginx config file
server {
listen 80;
server_name local.symfony;
root /home/guest/symfony_demo/web;
rewrite ^/app\.php/?(.*)$ /$1 permanent;
try_files $uri #rewriteapp;
location #rewriteapp {
rewrite ^(.*)$ /app.php/$1 last;
}
# Deny all . files
location ~ /\. {
deny all;
}
location ~ ^/(app|app_dev)\.php(/|$) {
fastcgi_split_path_info ^(.+\.php)(/.*)$;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME
$document_root$fastcgi_script_name;
fastcgi_index app.php;
send_timeout 1800;
fastcgi_read_timeout 1800;
fastcgi_pass unix:/var/run/php5-fpm.sock;
}
# Statics
location /(bundles|media) {
access_log off;
expires 30d;
try_files $uri #rewriteapp;
}
error_log /var/log/nginx/project_error.log;
access_log /var/log/nginx/project_access.log;
}
On load of app in browser its throwing an error
502 Bad Gateway Error:No input file specified.
Error caught from error.log file:
FastCGI sent in stderr: "Unable to open primary script: /home/guest/symfony_demo/web/app.php (No such file or directory)" while reading response header from upstream, client: 127.0.0.1, server: local.symfony, request: "GET / HTTP/1.1", upstream: "fastcgi://unix:/var/run/php5-fpm.sock:", host: "local.symfony"
Can anyone help me to configure symfony app to app_dev config file.
Any thoughts??
fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
This line is the problem. If you remove it, your error disappears. You may then have Opcache problems due to Symfony using symlinks to the current project.
With $realpath_root$fastcgi_script_name, the web server looks at the "real" path for the PHP script based on the root definition in your server block. Nginx must have read permissions to the path for this to work. Your code is in /home/guest/, if nginx is running as "www-data" give it permissions to your directories, or run nginx as the "guest" user (ignoring the security implications of this).
Why don't you start with configuration from official documentation (http://symfony.com/doc/current/cookbook/configuration/web_server_configuration.html#nginx) and when that works, you can try to add your custom configuration (caching of sttaic files ...) ?
This should work:
server {
listen 80;
server_name local.symfony;
root /home/guest/symfony_demo/web;
location / {
try_files $uri /app_dev.php$is_args$args;
}
location ~ ^/(app_dev|config)\.php(/|$) {
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_split_path_info ^(.+\.php)(/.*)$;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
fastcgi_param DOCUMENT_ROOT $realpath_root;
}
error_log /var/log/nginx/project_error.log;
access_log /var/log/nginx/project_access.log;
}
This error happens when nginx is not able to find the php-fpm.sock file.
Can you make sure that the php-fpm.sock file is in the path as mentioned. I had to update fastcgi_pass like below
fastcgi_pass unix:/var/run/php-fpm/php-fpm.sock;
as my php-fpm.sock was there
I've setup gitlab using the omnibus package on CentOS 7. I'd like to use the gitlab server to host additional websites. I've enabled custom nginx conf by adding the below code to /etc/gitlab/gitlab.rb
nginx['custom_nginx_config'] = "include /etc/nginx/conf.d/*.conf;"
I've also created conf files in /etc/nginc/conf.d. Static HTML files are working but when i try to run php scripts, I'm getting a File not found - 404 error.
Following is the nginx conf for php :
server{
listen 80;
server_name example.com;
root /var/www/vhosts/example;
index index.php index.html index.htm;
location / {
try_files $uri $uri/ =404;
}
error_page 404 /404.html;
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /opt/gitlab/embedded/html;
}
location ~ \.php$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
include /opt/gitlab/embedded/conf/fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
}
The following is the error log:
FastCGI sent in stderr: "Primary script unknown" while reading response from upstream, client x.x.x.x, server: example.com, request: "GET / HTTP/1.1", upsteam: "fastcgi://127.0.0.1:9000", host: "example.com"
Maybe your problem comes from your "location ~ .php$" config.
You already fix the first problem with gitlab omnibus by included the right fatscgi_params instead of the default. Now it seems to comes from the location config.
Try the following code for your configuration :
location ~ \.php$ {
#in your case maybe : /opt/gitlab/embedded/html
root [YOUR APP DIRECTORY];
try_files $uri $uri/ /index.php?$args;
index index.html index.htm index.php;
fastcgi_param PATH_INFO $fastcgi_path_info;
fastcgi_param PATH_TRANSLATED $document_root$fastcgi_path_info;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_intercept_errors on;
include /opt/gitlab/embedded/conf/fastcgi_params;
}
This fix works for me on a Debian 8 server.
Hope it could help.
Calling "http://test.local/" in the browser returns a "403 Forbidden" error.
Calling "http://test.local/index.php" works well.
If I create a "index.html" file, calling "http://test.local/" shows me the "index.html" file correctly.
So just the "index.php" file doesn't work.
This is my vhost configuration:
server {
listen 80;
server_name test.local;
root /var/www/public;
index index.html index.htm index.php;
location ~ \.php$ {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
}
The error.log file contains this error:
2013/12/28 14:00:57 [error] 2854#0: *50 directory index of "/var/www/public/" is forbidden, client: 10.0.2.2, server: test.local, request: "GET / HTTP/1.1", host: "test.local"
Why's that? I've googled a lot now and checked permissions (which are all set to 777) and tried lots of other stuff too, but I don't get it working.
I'm new to nginx so probably I'm just missing something.
Thanks a lot!
Your configuration seems fine, but I think it can be simplified, try this
server {
listen 80;
server_name test.local;
root /var/www/public;
index index.php;
location ~ \.php$ {
include fastcgi_params;
fastcgi_pass unix:/var/run/php5-fpm.sock;
}
}