Nginx multiple root redirect issue - redirect

I've the following configuration which works well:
root /var/www/en;
location / {
try_files $uri $uri/ /index.php?$args;
}
location /admin {
try_files $uri $uri/ /admin/index.php;
}
location ~ \.php(/|\?|$) {
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME
$document_root$fastcgi_script_name;
fastcgi_param COCKPIT_URL_REWRITE On;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/run/php/php7.0-fpm.sock;
fastcgi_index index.php;
}
This redirects all requests to index.php and to admin/index.php for the admin backend.
Now I need to put a copy of the website in a different language to subdirectory having the same redirect rules for the subdirectory. This is actually a separate project with a separate root folder. I'm doing it the following way:
location ~ ^/de {
root /var/www/de;
try_files $uri $uri/ /de/index.php?$args;
}
location ~ ^/de/admin {
root /var/www/de;
try_files $uri $uri/ /de/admin/index.phpd;
}
However, this leads to the following nginx error:
rewrite or internal redirection cycle while internally redirecting to "/de/index.php"
How this could be fixed?
Thanks ;)
UPDATE:
I managed to get static files working by changing root to alias. So, now I can access static files site.com/de/test.txt. However, when I try to access site.com/de/ php handler is not working and browser tries to download the php file. I put php handler inside the new location block but it still not working.

Try this
location ~ \.php(/|\?|$) {
location /de {
root /var/www/de;
try_files $uri $uri/ /de/index.php?$args;
}
location /de/admin {
root /var/www/de;
try_files $uri $uri/ /de/admin/index.php;
}
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME
$document_root$fastcgi_script_name;
fastcgi_param COCKPIT_URL_REWRITE On;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/run/php/php7.0-fpm.sock;
fastcgi_index index.php;
}

Related

nginx config - negative condition

I make HTTP authorization in nginx config for all files, exclude index.php:
server {
listen 80;
root /var/www/site;
index index.php;
charset utf-8;
location ~ /(?!index.php) {
auth_basic "Unauthorized";
auth_basic_user_file /var/www/site/htpasswd;
}
location ~ \.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;
fastcgi_read_timeout 3000;
}
}
As a result, nginx instead of executing PHP files, begins to give them to download. Without location ~ /(?! index.php) - files are normally executed.
How to make a negative condition correctly?

How to configure Zend Framework project which contains different modules in winginx server

Can any one show how to configure zend framework project in winginx using virtual host? I tried this, but running some issues when I go through the different modules. My server block looks like this:
server {
listen 127.0.0.1:80;
server_name experts.localhost;
root C:/Projects/project1/public;
index index.php index.html;
location / {
if (-f $request_filename) {
root C:/Projects/project1/public;
}
}
location ~ \.php$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root/$fastcgi_script_name;
include fastcgi_params;
}
}

Nginx 404's aren't working - They just refresh the page

My 404 page https://epicmc.us/404.php loads perfectly fine if you type it in directly, but when you get a 404 error while browsing a page like https://epicmc.us/thispagedoesntexist (doesn't exist) it just kinda refreshes the page.. Almost like I'm typing in https://epicmc.us/#
This is my config.
server {
listen 80;
listen 443;
default_type text/html;
location / {
root /usr/share/nginx/html;
index index.php index.html index.htm;
try_files $uri $uri/ /index.php;
}
root /usr/share/nginx/html;
index index.php index.html index.htm;
server_name epicmc.us;
error_page 404 /usr/share/nginx/html/404.php;
error_page 500 502 503 504 /50x.php;
location = /50x.html {
root /usr/share/nginx/html;
}
# pass the PHP scripts to FastCGI server listening on the php-fpm socket
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;
}
}

How to remove trailing slash from URL in nginx only if directory doesn't exist?

I am running a server on nginx 1.4.1 with PHP-FastCGI. Currently I have it setup so that it removes trailing slashes from my URLs and issues a 301 redirect. However, when I visit a directory that exists, I am forced into a redirect loop. My current document root looks like this:
- index.php (app)
- webgrind
- index.php
- static
- css
Currently I cannot visit example.com/webgrind or any other directory. My access logs repeatedly read similar to:
GET /webgrind/ HTTP/1.1" 301 178 "-"
GET /webgrind HTTP/1.1" 301 178 "-"
This is the server block in my nginx.conf:
server {
listen 80;
server_name example.com;
location / {
try_files $uri $uri/ /index.php?$args;
root /var/www/example/public;
index index.php index.html index.htm;
}
rewrite ^/(.*)/$ /$1 permanent;
location = /favicon.ico {
access_log off;
log_not_found off;
}
location ~ \.php$ {
try_files $uri $uri/ /index.php?$args;
root /var/www/example/public;
index index.php index.html index.htm;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /var/www/example/public$fastcgi_script_name;
fastcgi_param APPLICATION_ENV testing;
fastcgi_param PATH /usr/bin:/bin:/usr/sbin:/sbin;
fastcgi_intercept_errors on;
include fastcgi_params;
}
}
I am aware that rewrite ^/(.*)/$ /$1 permanent; is the offending line. If I remove it and visit example.com/webgrind, a 301 is issued for me to redirect to example.com/webgrind/ since it is a directory. However, my application will now accept both trailing and non-trailing slashes (i.e. example.com/users/ and example.com/users) and this is not what I want.
Wrapping the 'if' directive around my rewrite as follows still creates a redirect loop for my directories (if is evil, apparently, but a rewrite directive in this case is considered safe):
if (!-d $request_filename) {
rewrite ^/(.*)/$ /$1 permanent;
}
(I know that visiting webgrind/index.php would solve my problem, but I'd like to avoid costly and unprofessional redirect loops when my production directories are pushed live.)
So how can I conditionally strip trailing slashes only for resources that don't exist (my web application paths)?
UPDATE: My (unaltered) fastcgi_params config:
fastcgi_param QUERY_STRING $query_string;
fastcgi_param REQUEST_METHOD $request_method;
fastcgi_param CONTENT_TYPE $content_type;
fastcgi_param CONTENT_LENGTH $content_length;
fastcgi_param SCRIPT_FILENAME $request_filename;
fastcgi_param SCRIPT_NAME $fastcgi_script_name;
fastcgi_param REQUEST_URI $request_uri;
fastcgi_param DOCUMENT_URI $document_uri;
fastcgi_param DOCUMENT_ROOT $document_root;
fastcgi_param SERVER_PROTOCOL $server_protocol;
fastcgi_param GATEWAY_INTERFACE CGI/1.1;
fastcgi_param SERVER_SOFTWARE nginx/$nginx_version;
fastcgi_param REMOTE_ADDR $remote_addr;
fastcgi_param REMOTE_PORT $remote_port;
fastcgi_param SERVER_ADDR $server_addr;
fastcgi_param SERVER_PORT $server_port;
fastcgi_param SERVER_NAME $server_name;
fastcgi_param HTTPS $https;
# PHP only, required if PHP was built with --enable-force-cgi-redirect
fastcgi_param REDIRECT_STATUS 200;
Putting the root directive outside of the location block as a direct child of the server block fixed the issue.
server {
listen 80;
server_name example.com;
# This WORKS!
root /var/www/example/public;
location / {
try_files $uri $uri/ /index.php?$args;
index index.php index.html index.htm;
}
if (!-d $request_filename) {
rewrite ^/(.*)/$ /$1 permanent;
}
location = /favicon.ico {
access_log off;
log_not_found off;
}
location ~ \.php$ {
try_files $uri $uri/ /index.php?$args;
index index.php index.html index.htm;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /var/www/example/public$fastcgi_script_name;
fastcgi_param APPLICATION_ENV testing;
fastcgi_param PATH /usr/bin:/bin:/usr/sbin:/sbin;
fastcgi_intercept_errors on;
include fastcgi_params;
}
}
Apparently it is a pitfall that the Nginx wiki recommends to avoid.

NGINX configuration. PHP frameworks with PATHINFO 404

I usually use apache and want to give NGINX a try.
I have installed it on my ubuntu dev machine and have a few different frameworks and sites set up and in development (codeigniter, symfony, laravel, etc).
The problem I'm getting is that only paths that end with .php work. If I try index.php/welcome/index it just 404s instead of loading index.php.
I have tried with cgi.fix_pathinfo set to 1 and 0.
Here is my current (of many tried) site config.
server {
listen 80; ## listen for ipv4; this line is default and implied
#listen [::]:80 default_server ipv6only=on; ## listen for ipv6
root /my/path;
index index.php index.html;
# Make site accessible from http://localhost/
server_name localhost;
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
#error_page 500 502 503 504 /50x.html;
#location = /50x.html {
# root /usr/share/nginx/www;
#}
location ~ \.php$ {
try_files $uri =404;
# Fix for server variables that behave differently under nginx/php-fpm than typically expected
fastcgi_split_path_info ^(.+\.php)(/.+)$;
# Include the standard fastcgi_params file included with nginx
include fastcgi_params;
fastcgi_param PATH_INFO $fastcgi_path_info;
fastcgi_index index.php;
# Override the SCRIPT_FILENAME variable set by fastcgi_params
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
# Pass to upstream PHP-FPM; This must match whatever you name your upstream connection
fastcgi_pass unix:/var/run/php5-fpm.sock;
}
location / {
# First attempt to serve request as file, then
# as directory, then fall back to displaying a 404.
try_files $uri $uri/ =404;
# Uncomment to enable naxsi on this location
# include /etc/nginx/naxsi.rules
}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
location ~ /\.ht {
deny all;
}
}
I'd prefer to use the following nginx config structure. It's cleaner:
location / {
try_files $uri $uri/ #phpsite;
}
location #phpsite {
include fastcgi_params;
... other fast_cgi directives
}
A bit more complex setup can be found in the popular silex project: http://silex.sensiolabs.org/doc/web_servers.html#nginx.
I see 2 problems in the original config file:
location ~ \.php$ {
try_files $uri =404;
...
}
In regex '$' means matching at the end of the string. So it failed as stated in the comments by prodigitalson.
That try_files directive inside the above fast_cgi location block should not be there because that location block is supposed to be handled by php alone. It's cleaner to remove that line.
I think what you're missing is a rule like
location / {
try_files $uri $uri/ /index.php?$args;
}
that will try to call that index.php url if the path does not exist.
Or maybe, if you know that it is pointless to try other things, just
location / {
try_files /index.php?$args;
}
or
location ~ /index.php {
try_files /index.php?$args;
}
This works for me...
location ~ ^(.*?\.php)($|/.+) {
try_files $1 =404;
... fastcgi conf...
}