Nginx is downloading PHP files when trying to remove the extension - php

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.

Related

nginx $document_root with subdirectories insert the subdirectory in its path

I'm working with subdirectories. I want "babylon/webmail" to go to my rainloop webmail client.
location ^~ /webmail {
root /srv/rainloop/public_html;
try_files $uri $uri/ /webmail/index.php?$query_string;
access_log /srv/rainloop/logs/access.log;
error_log /srv/rainloop/logs/error.log;
index index.php;
access_log /var/log/nginx/scripts.log scripts;
location ~ \.php$ {
#fastcgi_index index.php;
#fastcgi_split_path_info ^(.+\.php)(.*)$;
#fastcgi_keep_conn on;
#include /etc/nginx/fastcgi_params;
#fastcgi_pass unix:/var/run/php5-fpm.sock;
#fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
#try_files $uri =404;
include fastcgi_params;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
#fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param SCRIPT_FILENAME /srv/rainloop/public_html/index.php;
#include fastcgi_params;
}
location ~ /\.ht {
deny all;
}
location ^~ /webmail/data {
deny all;
}
}
However this
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
doesn't work at all. It prints out: /srv/rainloop/public_html/webmail/index.php; That file doesn't exist in the directory structure, but: /srv/rainloop/public_html/index.php
fastcgi_param SCRIPT_FILENAME /srv/rainloop/public_html/index.php;
P.S.: After hardcoding, I don't get any error at all, but the page is blank with some rainloop code source code.
The path to the file is calculated by concatenating the value of root to the URI. The URI contains /webmail/index.php, otherwise it would not match the location block.
You probably mean to use alias instead of root as that directive removes the value of the prefix location when calculating the path to the file. See this document for details.
location ^~ /webmail {
alias /srv/rainloop/public_html;
if (!-e $request_filename) { rewrite ^ /webmail/index.php last; }
...
location ~ \.php$ {
if (!-f $request_filename) { return 404; }
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $request_filename;
fastcgi_pass unix:/var/run/php5-fpm.sock;
}
Avoid using try_files and alias in the same block, due to this long term issue, and see this caution on the use of if. Use $request_filename for the SCRIPT_FILENAME value.

Nginx location configuration (subfolders)

lets say I've a path like:
/var/www/myside/
that path contains two folders... let's say
/static and /manage
I'd like to configure nginx to have an access to:
/static folder on / (eg. http://example.org/)
this folder has some .html files.
/manage folder on /manage (eg. http://example.org/manage) in this case this folder contains Slim's PHP framework code - that means the index.php file is in public subfolder (eg. /var/www/mysite/manage/public/index.php)
I've tried a lot of combinations such as
server {
listen 80;
server_name example.org;
error_log /usr/local/etc/nginx/logs/mysite/error.log;
access_log /usr/local/etc/nginx/logs/mysite/access.log;
root /var/www/mysite;
location /manage {
root $uri/manage/public;
try_files $uri /index.php$is_args$args;
}
location / {
root $uri/static/;
index index.html;
}
location ~ \.php {
try_files $uri =404;
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_index index.php;
fastcgi_pass 127.0.0.1:9000;
}
}
The / works correctly anyway manage doesn't. Am I doing something wrong? Does anybody know what should I change?
Matthew.
To access a path like /var/www/mysite/manage/public with a URI like /manage, you will need to use alias rather than root. See this document for details.
I am assuming that you need to run PHP from both roots, in which case you will need two location ~ \.php blocks, see example below. If you have no PHP within /var/www/mysite/static, you can delete the unused location block.
For example:
server {
listen 80;
server_name example.org;
error_log /usr/local/etc/nginx/logs/mysite/error.log;
access_log /usr/local/etc/nginx/logs/mysite/access.log;
root /var/www/mysite/static;
index index.html;
location / {
}
location ~ \.php$ {
try_files $uri =404;
fastcgi_pass 127.0.0.1:9000;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $request_filename;
fastcgi_param SCRIPT_NAME $fastcgi_script_name;
}
location ^~ /manage {
alias /var/www/mysite/manage/public;
index index.php;
if (!-e $request_filename) { rewrite ^ /manage/index.php last; }
location ~ \.php$ {
if (!-f $request_filename) { return 404; }
fastcgi_pass 127.0.0.1:9000;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $request_filename;
fastcgi_param SCRIPT_NAME $fastcgi_script_name;
}
}
}
The ^~ modifier causes the prefix location to take precedence over regular expression locations at the same level. See this document for details.
The alias and try_files directives are not together due to this long standing bug.
Be aware of this caution in the use of the if directive.

nginx 403 forbidden error + mac + laravel

Here is my nginx's content.
My current access url is http://localhost/lampi/, and I am receiving response 403 forbidden.
My server's documentroot is /Library/WebServer/Documents/.
When I access http://localhost/, it shows ok. I can also see the index.html page's content.
I don't know what the matter is. I have checked top 10 pages in stackoverflow.
server {
server_name localhost;
access_log /var/log/nginx/nginx.host.access.log main;
root /Library/WebServer/Documents/;
location / {
#root html;
index index.html index.htm index.php;
}
location /lampi {
#autoindex on;
if (!-e $request_filename){
rewrite ^/lampi/(.*)$ /lampi/index.php?s=$1 last;
}
}
location ~ \.php$ {
include /usr/local/etc/nginx/fastcgi_params;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /Library/WebServer/Documents/lampi/$fastcgi_script_name;
}
location ~ [^/]\.php(/|$) {
fastcgi_split_path_info ^(.+?\.php)(/.*)$;
if (!-f $document_root$fastcgi_script_name) {
return 404;
}
fastcgi_param HTTP_PROXY "";
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
include fastcgi_params;
}
#location /images/ {
# root /usr/local/var/www;
#}
}
There are three potential problems with your configuration file.
The use of if (!-e $request_filename) is causing problems because it checks for the existence of directories, and you should probably be using try_files anyway (see this document for details):
location /lampi {
try_files $uri $uri/ #lampi;
}
location #lampi {
rewrite ^/lampi/(.*)$ /lampi/index.php?s=$1 last;
}
The value of SCRIPT_FILENAME adds an extra /lampi into the pathname. Use either of (both evaluate to the same value in this case):
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param SCRIPT_FILENAME $request_filename;
For example:
location ~ \.php$ {
try_files $uri =404;
include /usr/local/etc/nginx/fastcgi_params;
fastcgi_pass 127.0.0.1:9000;
fastcgi_param SCRIPT_FILENAME $request_filename;
}
The location ~ [^/]\.php(/|$) block is confusing. Normally it is adequate to use either location ~ \.php$ or something like location ~ [^/]\.php(/|$) depending on whether or not your application uses PATH_INFO. Delete the block you are not using. See this document for details.

Redirecting to controller/action

I want to be able to convert the following link:
http://domain.com/foo/bar
into http://domain.com/index.php?controller=foo&action=bar
using php5-fpm. I also want to be able to access static files inside www/ folder. How do I do that? This is what I have so far:
server {
charset utf-8;
client_max_body_size 8M;
listen 80; ## listen for ipv4
server_name domain.com;
root /var/www/domain.com/www;
index index.php;
access_log /var/log/nginx/domain.com.access.log;
error_log /var/log/nginx/domain.com.error.log;
location / {
rewrite ^([a-zA-Z0-9]+)/([a-zA-Z0-9]+)/?$ /index.php?controller=$1&action=$2;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
include fastcgi_params;
}
location ~ /\.(ht|svn|git) {
deny all;
}
}
But it gives me blank page and no get parameters. How should I do this?
The try_files directive is useful when serving static files, if they exist, and rewriting the URI if they do not. See this document for details.
There are a number of ways to achieve this, for example:
location / {
try_files $uri $uri/ #rewrite;
}
location #rewrite {
rewrite ^([a-zA-Z0-9]+)/([a-zA-Z0-9]+)/?$ /index.php?controller=$1&action=$2 last;
return 404;
}
location ~ \.php$ {
try_files $uri =404;
fastcgi_pass unix:/var/run/php5-fpm.sock;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $request_filename;
}

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;
}
}

Categories