Nginx try_files index.php fails to get processed - wordpress

I have this config setup:
location / {
#try_files $uri /index.php$is_args$args; <-- works as expected
#try_files $uri /index.php$is_args$args /maintanence.html; <-- does not work
#try_files $uri /maintanence.html; < -- switches to maintenance page as expected
try_files $uri /index.php /maintanence.html; <-- also does not work
}
location ~ \.php$ {
include fastcgi.conf;
fastcgi_intercept_errors on;
fastcgi_pass unix:/run/php/php7.3-fpm.sock;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
1st and 3rd lines work 2nd and last lines do not.
I have noticed that whenever you put any other filename after index.php it just returns raw index.php contents in response.
Can someone explain why this happens?

Your comment states that you wish to deliver maintenance.html if the file index.php does not exist.
The try_files statement that checks for the existence of index,php must be in the same location that processes the request.
You could do it in the location ~ \.php$ block if you want the non-existence of any .php file to trigger maintenance.html, but the solution below will limit the check to index.php only.
For example:
location / {
try_files $uri /index.php$is_args$args;
}
location = /index.php {
try_files $uri /maintanence.html;
include fastcgi.conf;
fastcgi_intercept_errors on;
fastcgi_pass unix:/run/php/php7.3-fpm.sock;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
location ~ \.php$ {
try_files $uri =404;
include fastcgi.conf;
fastcgi_intercept_errors on;
fastcgi_pass unix:/run/php/php7.3-fpm.sock;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}

Related

Multiple projects for one domain nginx

I need to setup two projects under one domain(react+symfony), here is my nginx config:
server {
listen 80;
server_name domain.ltd;
rewrite_log on;
root /var/www/frontend;
access_log /var/log/nginx/project_access.log;
error_log /var/log/nginx/project_error.log;
location / {
index /index.html;
}
location /api/ {
alias /var/www/backend;
try_files $uri /index.php$is_args$args;
location ~ \.php$ {
fastcgi_pass 127.0.0.1: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;
}
}
}
root / works fine so static index.html I'm getting okay without any issues. but symfony application for /api throws an error(403 Forbidden) in nginx error.log:
32349 open() "/var/www/frontend/index.php"
For some reason alias ignored, what i'm doing wrong?
Thanks in advance
The location and the alias values should both end with a / or neither end with a /. The last parameter of the try_files statement is a URI, so you need to include the /api/ prefix.
For example:
location /api/ {
alias /var/www/backend/;
try_files $uri /api/index.php$is_args$args;
location ~ \.php$ {
try_files $uri =404;
fastcgi_pass 127.0.0.1:9000;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $request_filename;
fastcgi_param DOCUMENT_ROOT $realpath_root;
internal;
}
}
Also, add a try_files statement to the nested location block to avoid passing uncontrolled requests to PHP. The correct value for SCRIPT_FILENAME is $request_filename which works with either root or alias.

Conditionally prepend path to url in NGINX

I have this directory structure for my projects
/var/www
/project-a
/data <-- configuration and user files/data
/app <-- all the code is in sub-dirs in here
/pm <-- a home brew project management app
/.pmdata <-- data used by pm
My goal is to configure NGINX so I can access the project itself through
http://project-a.dev/ and the project management with http://project-a.dev/pm/.
In other words, I want the second url preserved as is, but if the url does not point to /pm/* it should be re-written to have the missing /app prepended to it.
I have tried the following configuration but http://project-a.dev/pm/ results in 404 and http://project-a.dev/ first redirects to http://project-a.dev/app/ and then gives 404.
What am I doing wrong?
server {
listen 127.0.0.1:80;
root /var/www/project-a;
index index.php index.html index.htm;
server_name project-a.dev;
location / {
try_files $uri $uri/app $uri/app/ =404;
}
location ~ \.php$ {
try_files $uri =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;
}
}
Alternatively you could append /app to the root value for all URIs that do not begin with /pm. For example:
server {
...
root /var/www/project-a/app;
index index.php index.html index.htm;
location / {
try_files $uri $uri/ =404;
}
location ~ \.php$ {
try_files $uri =404;
fastcgi_pass unix:/var/run/php5-fpm.sock;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $request_filename;
}
location ^~ /pm {
root /var/www/project-a;
try_files $uri $uri/ =404;
location ~ \.php$ {
try_files $uri =404;
fastcgi_pass unix:/var/run/php5-fpm.sock;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $request_filename;
}
}
}
The nested location block for location ~ \.php$ executes PHP files within the /pm hierarchy. The ^~ modifier is necessary to avoid the other location ~ \.php$ block taking control. See this document for details.

using alias to send certain URL to different php directory

I am currently sending all request with '.php' to the php engine using the following:
root /var/www/public;
index index.php;
location / {
try_files $uri $uri/ /index.php;
}
location ~ \.php$ {
try_files $uri =404;
fastcgi_pass php:9000;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PHP_VALUE "error_log=/var/log/nginx/php_errors.log";
fastcgi_buffers 16 16k;
fastcgi_buffer_size 32k;
}
I would like to get all requests to /login/... to be sent to a different directory on the php server. I gather that alias and try_files don't work together. I have tried using another 'root', but it doesn't work:
location ^~ /login/ {
root /var/www/login/etc/;
try_files $uri $uri/ /login$is_args$args;
}
location / {
try_files $uri $uri/ /index.php;
}
location ~ \.php$ {
try_files $uri =404;
fastcgi_pass php:9000;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PHP_VALUE "error_log=/var/log/nginx/php_errors.log";
fastcgi_buffers 16 16k;
fastcgi_buffer_size 32k;
}
How can I get this to work so all /login/... requests end up being served from /var/www/login/etc directory on php. Thanks.
Try this,
http {
...
map $request_uri $rot{
"~ /login" /var/www/login/etc/;
default <insert_default_root>;
}
server {
...
root $rot;
...
location / {...}
location ~ \.php$ {...}
...
}
...
}

Override location directives in nginx not working

I want to override the permissions to /folder/script.php and have the following rules:
location ^~ /folder/script.php{
allow all;
} #shouldn't ^this one with ^~ override the others?
location ~ /folder/(.+)\.php$ {
deny all;
return 404;
allow 127.0.0.1;
}
location ~ ^/folder {
return 404;
}
location / {
# First attempt to serve request as file, then
# as directory, then trigger 404
try_files $uri $uri/ =404;
server_name_in_redirect off;
}
location ~ \.php$ {
try_files $uri =404;
#fastcgi_split_path_info ^(.+\.php)(/.+)$;
# NOTE: You should have "cgi.fix_pathinfo = 0;" in php.ini
#fastcgi_pass /tmp/php5-fpm.sock;
#fastcgi_pass /var/run/php5-fpm.sock;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $root_folder$fastcgi_script_name;
fastcgi_param DOCUMENT_ROOT $root_folder;
# send bad requests
fastcgi_intercept_errors on;
include fastcgi_params;
}
but whenever I access admin.php I still get a 404 error and/or the script.php file is served to download, not interpreted. Could someone explain me why? Tyvm
The commands to execute a php script are:
try_files $uri =404;
fastcgi_pass 127.0.0.1:9000;
fastcgi_param SCRIPT_FILENAME $root_folder$fastcgi_script_name;
fastcgi_param DOCUMENT_ROOT $root_folder;
fastcgi_intercept_errors on;
include fastcgi_params;
I'm not sure where you define $root_folder, normally $document_root is used. The above (or similar) code must appear in each and every location block that is expected to execute php scripts.
So your configuration should look something like this:
location / {
try_files $uri $uri/ =404;
server_name_in_redirect off;
}
location ^~ /folder { deny all; }
location = /folder/script.php {
try_files $uri =404;
fastcgi_pass 127.0.0.1:9000;
fastcgi_param SCRIPT_FILENAME $root_folder$fastcgi_script_name;
fastcgi_param DOCUMENT_ROOT $root_folder;
fastcgi_intercept_errors on;
include fastcgi_params;
}
location ~ \.php$ {
try_files $uri =404;
fastcgi_pass 127.0.0.1:9000;
fastcgi_param SCRIPT_FILENAME $root_folder$fastcgi_script_name;
fastcgi_param DOCUMENT_ROOT $root_folder;
fastcgi_intercept_errors on;
include fastcgi_params;
}
I have taken the liberty of simplifying your configuration a little. It seems that only the file /folder/script.php is executable within the /folder hierarchy, so an exact match (location =) is used, and /folder is denied. The allow 127.0.0.1; does nothing as it comes after the deny all.
As you can see, the fastcgi_pass 127.0.0.1:9000; directive must appear in any location container that directly handles php code. I would put some or all of these directives into a separate file and use include to pull them in at each location.
The rest of the directives have been copied across from your question, but I don't know if they are required here.

Nginx location for PHP files that do not exist

I have config(minimal):
server {
listen test.local:80;
server_name test.local;
server_name_in_redirect off;
location / {
root /data/www/test/public;
try_files $uri $uri/ /index.php?route=$uri&$args;
index index.html index.php;
}
location ~ \.php$ {
root /data/www/test/public;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /data/www/test/public$fastcgi_script_name;
fastcgi_param ...
...
}
}
Thats work file for urls /help/ or /contacts/ etc… (all redirect to index.php with get variables).
But if url, for example, /help.php or contacts.php, and this files not exists, I have output:
File not found.
How update my Nginx config? I need URLs, for example:
/help.php => /index.php?route=/help.php
/contacts.php?foo=bar... => /index.php?route=/contacts.php&args=…
Here is simple config for 404 error page . Its custom 404 page.
error_page 404 /404.php;
location /404.php {
root /var/www/public_html;
allow all;
}
Last part
Important line is ========== try_files $uri =404;
location ~ .php$ {
try_files $uri =404;
fastcgi_pass 127.0.0.1:9001;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /var/www/public_html$fastcgi_script_name;
include fastcgi_params;
}

Resources