I am trying to setup a specific redirection to force redirection to index.php (for Laravel) for a specific subdirectory, bypassing existing index.html.
The url i want to catch looks like this app/kit//?email= . In each directory there's a index.html file (and for some business reasons it's hard to change this)
The redirection seems to work, but when i parse $_SERVER in index.php, i lose the query string.
My Nginx configuration look like this:
server {
server_name somedomain.com;
root /home/www/preprod/current/public/;
rewrite_log on;
access_log /var/log/nginx/preprod-access.log;
error_log /var/log/nginx/preprod-error.log notice;
location ~* /kit/(.*)/index\.html {
error_log /var/log/nginx/preprod-kit-error.log debug;
try_files /index.php?$query_string /dev/null;
}
location / {
index index.php index.html;
try_files $uri $uri/ /index.php?$query_string;
client_max_body_size 0;
autoindex off;
allow all;
}
location ~ \.php$ {
error_log /var/log/nginx/preprod-php-error.log debug;
#try_files $uri =404;
#fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_intercept_errors off;
fastcgi_connect_timeout 300s;
fastcgi_read_timeout 15m;
fastcgi_send_timeout 600s;
fastcgi_keep_conn on;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
include fastcgi_params;
client_max_body_size 0;
include snippets/fastcgi-php.conf;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
}
All the application works fine, except this one.
Edit 1: snippets/fastcgi-php.conf
# regex to split $uri to $fastcgi_script_name and $fastcgi_path
fastcgi_split_path_info ^(.+\.php)(/.+)$;
# Check that the PHP script exists before passing it
try_files $fastcgi_script_name =404;
# Bypass the fact that try_files resets $fastcgi_path_info
# see: http://trac.nginx.org/nginx/ticket/321
set $path_info $fastcgi_path_info;
fastcgi_param PATH_INFO $path_info;
#fastcgi_index index.php;
include fastcgi.conf;
I found a solution,i used this :
location ~* /kit/(.*)/(.*) {
error_log /var/log/nginx/preprod.adsconsole.com-kit-error.log debug;
rewrite /kit/(.*)/(.*) /index.php$is_args$args;
}
The regexp can obviously be better written, but if it can help someone !
Related
I'am running ddev under macos with apple silicon m1.
I'am trying to open a phar (Contao-Manager.phar.php) file in the browser but got a 404.
webroot is correctly set, because its is possible to open a test.php in same webroot in the browser.
Here Is My nginx-conf:
server {
listen 80 default_server;
listen 443 ssl default_server;
root /var/www/html/web;
ssl_certificate /etc/ssl/certs/master.crt;
ssl_certificate_key /etc/ssl/certs/master.key;
include /etc/nginx/monitoring.conf;
index index.php index.htm index.html;
sendfile off;
error_log /dev/stdout info;
access_log /var/log/nginx/access.log;
location / {
absolute_redirect off;
try_files $uri $uri/ /index.php?$query_string;
}
location #rewrite {
rewrite ^ /index.php;
}
location ~ \.php$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+?\.php)(/.*)$;
fastcgi_pass unix:/run/php-fpm.sock;
fastcgi_buffers 16 16k;
fastcgi_buffer_size 32k;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param SCRIPT_NAME $fastcgi_script_name;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_intercept_errors off;
fastcgi_read_timeout 10m;
fastcgi_param SERVER_NAME $host;
fastcgi_param HTTPS $fcgi_https;
}
location ~* /\.(?!well-known\/) {
deny all;
}
location ~* (?:\.(?:bak|conf|dist|fla|in[ci]|log|psd|sh|sql|sw[op])|~)$ {
deny all;
}
include /etc/nginx/common.d/*.conf;
include /mnt/ddev_config/nginx/*.conf;
}
Try to change your default location to this (didn't check if works):
location / {
absolute_redirect off;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
try_files $fastcgi_script_name $uri $uri/ /index.php?$query_string;
}
And check your access.log and error.log if it's not working.
The problem is somewhere between fastcgi_split_path_info and your location order (which location actually handles *.php request first).
It is also possible that your regular expression in fastcgi_split_path_info doesn't match Contao-Manager.phar.php (file has 2 extensions).
I want to move my root project to subfolder.
My current config is:
server {
listen 80;
index index.php index.html;
error_log /var/log/nginx/error.log;
access_log /var/log/nginx/access.log;
root /var/www/public;
location ~ \.php$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass app:9000;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info;
}
location / {
try_files $uri $uri/ /index.php?$query_string;
gzip_static on;
}
}
And current project structure as expected:
/var/www/public
﹂index.php
The problem is to rewrite locations ~ \.php$ and / with new variable of subfolder (project name).
/var/www/new-project/public
﹂index.php
I've tried to rewrite locations but it didn't work:
server {
listen 80;
index index.php index.html;
error_log /var/log/nginx/error.log;
access_log /var/log/nginx/access.log;
# root /var/www/public;
root /var/www;
location ~ ([a-zA-Z0-9_\/\.]+)\.php$ {
try_files $uri =404;
fastcgi_split_path_info ^$1(.+\.php)(/.+)$;
fastcgi_pass app:9000;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info;
}
location ~/([a-zA-Z0-9_\/\.]+)$ {
alias /var/www/$1/public;
try_files $uri $uri/ /index.php?$query_string;
gzip_static on;
}
}
I think second location is pretty valid but I can't get how to rewrite it for \.php$ ending.
Found some overcome for this problem:
server {
listen 80;
index index.php index.html;
error_log /var/log/nginx/error.log;
access_log /var/log/nginx/access.log;
root /var/www/;
location / {
deny all;
return 404;
}
location ~ /([a-zA-Z0-9-_]+)/(.+\.php$) {
alias /var/www/$1/public/$2;
# try_files $uri =404;
fastcgi_split_path_info ^/$1/(.+\.php)(/.+)$;
fastcgi_pass app:9000;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME /var/www/$1/public/$2/$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info;
}
location ~ /([a-zA-Z0-9-_]+)/$ {
alias /var/www/$1/public/;
# try_files $uri $uri/ /index.php?$query_string;s
gzip_static on;
}
}
But the only one disadvantage of this approach that you can not use try_files with the alias.
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;
}
I've got problem with my project in symfony2 when i'm trying to run this on nginx.
www.domain/ works fine
www.domain/app_dev.php ,"An error occurred while loading the web debug toolbar (404: Not Found).Do you want to open the profiler?"
www.domain/app.php/someurl/ - 404 not found
Is it server configuration issue or should I change my .htaccess in some way to make it possible to run with nginx?
You can check my dev config (with PHP-FPM):
server {
listen 80;
server_name dev.example.com;
root /var/www/web;
access_log /var/log/nginx/access.log;
error_log /var/log/nginx/error.log error;
index app.php index.html index.htm;
try_files $uri $uri/ #rewrite;
location #rewrite {
rewrite ^/(.*)$ /app.php/$1;
}
location ~ \.php(/|$) {
# try_files $uri =404;
fastcgi_index app.php;
fastcgi_pass unix:/var/run/php5-fpm.sock;
include fastcgi_params;
fastcgi_split_path_info ^(.+\.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_buffer_size 1280k;
fastcgi_buffers 4 2560k;
fastcgi_busy_buffers_size 2560k;
}
location ~ /\.ht {
deny all;
}
}
I've looks up and down and, while this has been answered dozens of times, I can't get it to work. I'm trying to get apache style multiviews on my PHP site running under nginx. In this case I don't care about all file extensions, just php. So i have my try_files directive:
try_files $uri $uri.php $uri/ $1?$args $1.php?$args
which is all good and dandy, except that when I visit a PHP page without the PHP file extension, the PHP doesn't get rendered and just gets dumped straight to the browser. I see why (PHP is only being used when the location ends in .php, but I've got no idea how to fix it. Here's my config:
server {
listen 80; ## listen for ipv4; this line is default and implied
#listen [::]:80 default ipv6only=on; ## listen for ipv6
root /usr/share/nginx/www;
index index.php index.html index.htm;
server_name inara.thefinn93.com;
location / {
root /usr/share/nginx/www;
try_files $uri $uri.php $uri/ $1?$args $1.php?$args;
}
location ~ ^(.+\.php)$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_index index.php;
include fastcgi_params;
}
location ~ /\.ht {
deny all;
}
}
In your scenario, the location / is the last processed location setting. Having a try_files on it won't make it past the location ~ ^(.+\.php)$ setting (unless it ends with ".php"), therefore not being forwarded to the fastcgi upstream. You might use a named location for that purpose (locations starting with "#").
Here's an example based on your configuration:
# real .php files only
location ~ ^(.+\.php)$ {
# try_files is not needed here. The files will be checked at "location /"
# try_files $uri =404;
# do not split here -- multiviews will be handled by "location #php"
# fastcgi_split_path_info ^(.+\.php)(/.+)$;
# fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_pass unix:/var/run/php5-fpm.sock;
# also not needed here/with try_files
# fastcgi_index index.php;
include fastcgi_params;
}
# pseudo-multiviews
location #php {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
fastcgi_index index.php;
fastcgi_pass unix:/var/run/php5-fpm.sock;
# search for the split path first
# "$uri/" is not needed since you used the index
try_files $fastcgi_script_name $uri.php =404;
}
# this should also be before "location /"
location ~ /\.ht {
deny all;
}
location / {
root /usr/share/nginx/www;
# if file does not exist, see if the pseudo-multiviews work
try_files $uri #php;
}