Nginx try_files for php file - php

I have nginx configuration
location / {
try_files $uri $uri/ /index.php?q=$uri&$args;
}
location ~ \.php$ {
try_files $uri /index.php?q=$uri&$args;
include conf.d/php-fpm.inc;
}
it works when i access mysite.com/nonexistent_file.ext, showing the content of index.php
but, accessing mysite.com/nonexistent_file.php (with ending php), show nginx 404 page.
Please help crafting the try_files rule for php file. Thank you.
UPDATE, conf.d/php-fpm.inc contains
location ~ \.php$ {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
Update:
Solved by removing location tag inside the conf.d/php-fpm.inc file.
Thank you everyone.

Related

Nginx + Laravel - Moving blog from subdomain to /blog

My server application uses Laravel, and I have until recently had a wordpress blog hosted at blog.server.com
I wish to change this to server.com/blog
I have updated the main nginx config by adding the following:
location ^~ /blog {
root /home/ploi/blog.server.com/public;
index index.php index.html index.htm;
try_files $uri $uri/ /blog/index.php?$query_string;
location ~ \.php$ {
try_files $uri /index.php =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/run/php/php7.4-fpm.sock;
fastcgi_buffers 16 16k;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
This kind of works. It loads the new /blog url and posts, but:
No static assets get loaded (js, png, css, etc)
server.com/blog/wp-admin/ loops infinitely
How can I solve this?
You could try this, worked for me:
location /blog {
alias /home/ploi/blog.server.com/public;
try_files $uri $uri/ #blogrewrite;
location ~ \.php$ {
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $request_filename;
fastcgi_pass unix:/run/php/php7.4-fpm.sock;
}
}
location #blogrewrite {
rewrite /blog/(.*)$ /blog/index.php?/$1 last;
}
I used a "named location" (#blogrewrite), but frankly I don't know why this was necessary, but it avoided the recursion.

Nginx is downloading PHP files when trying to remove the extension

So, basically my current configuration is like below. Whenever I try and request the URL without the PHP extension it downloads it instead?
server {
listen [::]:80;
root myDirectory;
index index.php index.html;
server_name myDomain;
location / {
try_files $uri $uri/ $uri.php $uri.php$is_args$query_string =404;
}
location ~\.php$ {
include snippets/fastcgi-php.conf;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param SCRIPT_NAME $fastcgi_script_name;
fastcgi_pass unix:/run/php/php7.1-fpm.sock;
}
location ~ /\ ht {
deny all;
}
}
I've already done what a lot of other answers in other questions have suggested like editing the php7.1-fpm php.ini file:
cgi.fix_pathinfo=0
Any ideas?
Your current use of try_files will not work. The file elements of the try_files statement are processed within the same location block, which is the wrong location for PHP files. See this document for more.
There are a number of solutions, but you could use a named location to perform an internal rewrite if the script file is found to exist.
For example:
location / {
try_files $uri $uri/ #rewrite;
}
location #rewrite {
if (-f $document_root$uri.php) { rewrite ^ $uri.php last; }
return 404;
}
location ~ \.php$ {
try_files $uri =404;
include snippets/fastcgi-php.conf;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param SCRIPT_NAME $fastcgi_script_name;
fastcgi_pass unix:/run/php/php7.1-fpm.sock;
}
See this caution on the use of if.

Configure nginx with two php-fpm fastcgi_pass locations

For wordpress site using nginx + php-fpm I'm interested in applying a longer fastcgi_read_timeout directive just to /wp-admin directory to avoid timeouts for time intensive admin tasks.
The only issue with the code sample below is when I visit http://webpage.org/wp-admin I get 404. When I visit http://webpage.org/wp-admin/index.php the page posts.
Using nginx add-header directive to help me debug I've been able to determine that when visiting http://webpage.org/wp-admin nginx chooses location ~ .php$ over location ^~ /wp-admin.
Any ideas on how to resolve this? Thanks
location ^/wp-admin/.*.(php|phps)$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php-fpm/php-fpm.sock;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root/$fastcgi_script_name;
fastcgi_keep_conn on;
fastcgi_read_timeout 120;
}
location ~ \.php$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php-fpm/php-fpm.sock;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root/$fastcgi_script_name;
fastcgi_keep_conn on;
}
I tested change to try_files directive suggested by Keenan Lawrence which resolved issue for me.
Helpful explanation of nginx try_files directive can be found here: how can i make this try_files directive work?
To troubleshoot this problem I used add_header directive, placing one directive in each location. Then with Chrome browser I opened Developer Tools, clicked on Network tab, clicked on Record Network Log, then loaded the test wp-admin/ page. There is a Header tab that you can then click on to verify where your page loaded. Also see https://serverfault.com/questions/404626/how-to-output-variable-in-nginx-log-for-debugging
Working configuration below that includes header directives for debug.
location ~* ^/wp-admin/.*.(php|phps)$ {
add_header X-debug-message "This page processed from location ^~ /wp-admin . uri = $uri ." always;
try_files $uri $uri/ =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php-fpm/php-fpm.sock;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root/$fastcgi_script_name;
fastcgi_keep_conn on;
fastcgi_read_timeout 120;
}
location ~ \.php$ {
add_header X-debug-message "This page processed from location ~ \.php uri = $uri ." always;
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php-fpm/php-fpm.sock;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root/$fastcgi_script_name;
fastcgi_keep_conn on;
}

nginx - codeigniter 404 error for all urls

I have uploaded my files(Codeigniter Framework) in abcd directory ( url : example.com/abcd ) . All the links inside give 404 error . For removing index.php, I have placed this nginx.conf file in my root directory ( on server : /.../abcd/)
Here is the code for the nginx.conf file :
server {
server_name example.com/abcd;
root /..../abcd;
index index.html index.php index.htm;
# set expiration of assets to MAX for caching
location ~* \.(ico|css|js|gif|jpe?g|png)(\?[0-9]+)?$ {
expires max;
log_not_found off;
}
location / {
# Check if a file exists, or route it to index.php.
try_files $uri $uri/ /index.php;
}
location ~* \.php$ {
fastcgi_pass example.com/abcd;
fastcgi_index index.php;
fastcgi_split_path_info ^(.+\.php)(.*)$;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
}
In apache, it was easy using rewrite, i am now stuck with nginx. Please help, I am using nginx for the first time
Try below code.. taken from
https://serverfault.com/questions/695521/codeigniter-in-subdirectory-on-nginx-404
location /abcde/ {
alias /var/www/abcde/;
try_files $uri $uri/ /abcde/index.php;
location ~ \.php$ {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_index index.php;
fastcgi_pass backend;
include /etc/nginx/fastcgi_params;
fastcgi_param SCRIPT_FILENAME $request_filename;
}
}

Nginx sucessfully password protects PHP files, but then prompts you to download them

In a previous question, I was trying to password protect my /admin/ and sub-folders directory using Nginx with .htpasswd and regex.
That was done successfully, but now, after password authentication was completed, Nginx prompts to "download" php files, rather than simply loading them.
This doesn't happen when the new location "authentication" block is commented out. For instance, in this code sample, PHP pages load without any issue:
location / {
try_files $uri $uri/ =404;
}
#location "~^/admin/.*$" {
# try_files $uri $uri/ =404;
# auth_basic "Restricted";
# auth_basic_user_file /etc/nginx/.htpasswd;
#}
location ~ \.php$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
How can I resolve these (apparently conflicting) location blocks, so the /admin/ section is password protected yet php files still load?
The problem is a fundamental misunderstanding as to how nginx processes a request. Basically, nginx chooses one location to process a request.
You want nginx to process URIs that begin with /admin in a location block that requires auth_basic. In addition, URIs that end with .php need to be sent to PHP7.
So you need two fastcgi blocks, one to process normal PHP files and one to process restricted PHP files.
There are several forms of location directive. You have already discovered that the regex locations are ordered and therefore your location "~^/admin/.*$" block effectively prevents the location ~ \.php$ block from seeing any URI beginning with /admin and ending with .php.
A clean solution would be to use nested location blocks and employ the ^~ modifier which forces a prefix location to take precedence over a regex location:
location / {
try_files $uri $uri/ =404;
}
location ~ \.php$ {
try_files $uri =404;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;
}
location ^~ /admin/ {
auth_basic "Restricted";
auth_basic_user_file /etc/nginx/.htpasswd;
try_files $uri $uri/ =404;
location ~ \.php$ {
try_files $uri =404;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;
}
}
Note that location ^~ is a prefix location and not a regex location.
Note also that the fastcgi_split_path_info and fastcgi_index directives are not required in a location ~ \.php$ block.

Categories