location redirection for pretty api url end - nginx-config

Below are two blocks in the /etc/nginx/sites-enabled/default one after the other as seen below :-
# First section
1. location / {
try_files $uri $uri/ /index.php?$query_string;
}
#Second location block -
2. location /pilot/(.*)/?$ {
#try_files $uri $uri/ /pilot?id=$1;
try_files $uri $uri/ /index.php?id=$1;
}
Even then when I hit http://localhost:8080/pilot/66 it just shows me the result as same as http://localhost:8080/pilot or http://localhost:8080/pilot/. It should have showed me result on for 66 (id=66) only. Thanks in advance!
Referred http://blog.martinfjordvald.com/2011/02/nginx-primer-2-from-apache-to-nginx/. from https://www.nginx.com/resources/wiki/start/index.html

Related

Nginx multiple root redirect issue

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

Nginx doesn't redirect "/" to "/index.php"

Nginx doesn't rewrite "/" endpoint to call index.php as default even after adding the redirect in /etc/nginx/sites-available/default.
location / {
# try_files $uri $uri/ =404;
try_files $uri $uri/ /index.php?q=$uri&$args;
#index index.html index.htm index.php;
#try_files $uri $uri/ /index.php?$args;
}
I get 403 Forbidden when I visit my-site.com, but it works correctly at my-site.com/index.php and so does mysite.com/phpmyadmin/index.php. Also, the redirect seems to work for index.html, but not index.php.
/etc/nginx/sites-available/default
server {
listen 80;
listen [::]:80;
server_name my-site.com www.my-site.com;
return 301 https://$server_name$request_url;
}
server {
listen 443 ssl http2;
listen [::]:443 ssl http2;
include snippets/ssl-my-site.com.conf;
include snippets/ssl-params.conf;
root /var/www/html;
index index.php index.html index.htm index.nginx-debian.html;
server_name my-site.com;
location / {
# try_files $uri $uri/ =404;
#try_files $uri $uri/ /index.php?q=$uri&$args;
try_files $uri $uri/ /index.php?q=$uri&$args;
#index index.html index.htm index.php;
#try_files $uri $uri/ /index.php?$args;
}
location ~ [^/].php(/|$) {
fastcgi_split_path_info ^(.+?.php)(/.*)$;
if (!-f /var/www/html$fastcgi_script_name) {
return 404;
}
include fastcgi_params;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /var/www/html$fastcgi_script_name;
#fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;
fastcgi_pass 127.0.0.1:9000;
fastcgi_read_timeout 300;
}
location ~ /.ht {
deny all;
}
location ~ /.well-known {
allow all;
}
location /phpmyadmin {
index index.php;
}
# Rewrite rules for WordPress Multi-site.
if (!-e $request_filename) {
rewrite /wp-admin$ $scheme://$host$uri/ permanent;
rewrite ^/[_0-9a-zA-Z-]+(/wp-.*) $1 last;
rewrite ^/[_0-9a-zA-Z-]+(/.*\.php)$ $1 last;
}
}
# redirect www to non-www
server {
listen 443;
listen [::]:443;
server_name www.my-site.com;
return 301 https://my-site.com$request_uri;
}
I recently had the same issue and this might be the solution:
location / {
try_files $uri $uri/index.php;
}

Nginx rewrite or internal redirection cycle while internally redirecting

I am having troubles with nginx.
Here is my config.
server {
listen 80;
listen [::]:80;
server_name www.test.local;
return 301 http://test.local$request_uri;
}
server {
server_name test.local;
root /usr/share/nginx/test/htdocs/web;
# error_log /var/log/nginx/test.error.log;
# access_log /var/log/nginx/test.access.log;
rewrite ^/app\.php/?(.*)$ /$1 permanent;
location / {
index app.php;
try_files $uri #rewriteapp;
}
location #rewriteapp {
rewrite ^(.*)$ /app.php/$1 last;
}
location ~ ^/app\.php(/|$) {
fastcgi_pass 172.17.0.1:48000;
fastcgi_split_path_info ^(.+\.php)(/.*)$;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
fastcgi_param DOCUMENT_ROOT $realpath_root;
internal;
}
location /uploads/ {
root /usr/share/nginx/test/htdocs/web/uploads;
try_files $uri $uri/;
access_log off;
expires 30d;
}
location /images/ {
root /usr/share/nginx/test/htdocs/web/images;
try_files $uri $uri/;
access_log off;
expires 30d;
}
location /css/ {
root /usr/share/nginx/test/htdocs/web/css;
try_files $uri $uri/;
access_log off;
expires 30d;
}
location /js/ {
root /usr/share/nginx/test/htdocs/web/js;
try_files $uri $uri/;
access_log off;
expires 30d;
}
location /fonts/ {
root /usr/share/nginx/test/htdocs/web/fonts;
try_files $uri $uri/;
access_log off;
expires 30d;
}
location = /favicon.ico {
return 204;
access_log off;
log_not_found off;
}
}
I want my locations as http://test.local/css/ link to /usr/share/nginx/test/htdocs/styles/ as i did it in my config.
But when im entering for example http://test.local/css/flow.css im getting next error:
2016/05/13 15:55:30 [error] 5#5: *64 rewrite or internal redirection cycle while internally redirecting to "/css/flow.css///////////", client: 192.168.99.1, server: test.local, request: "GET /css/flow.css HTTP/1.1", host: "test.local", referrer: "http://test.local/"
Whats the problem here?
You have two problems with your configuration.
The first problem is that your root directives are wrong, so the file is not found.
The second problem is that the default action is to add another / to the end of the URI.
You need to add a valid default action to your try_files directive, such as a 404 response:
try_files $uri $uri/ =404;
So, back to the first problem. Your configuration states that /css/example.css is located at /usr/share/nginx/test/htdocs/web/css/css/example.css. Notice that /css/ is replicated. Same goes for /images/, /js/ and /fonts/. See this document for details of the root directive.
In most configurations, it is sufficient to specify the root at the server block level and allow it to be inherited by almost all location blocks, rather than repeating it in each location.
Your question states that /css/ files should be found in /usr/share/nginx/test/htdocs/styles/. This requires an alias directive. See this document for details of the alias directive. For example:
location /css/ {
alias /usr/share/nginx/test/htdocs/styles/;
access_log off;
expires 30d;
}

How to only redirect urls located in a nginx Map

I am in the process of moving my blog from a.com to b.com. Now I want to tell Google /bookmarks that all blogposts (around 100) have been moved to b.com. I only want to redirect the blogposts and nothing else.
After reading about the map module in nginx I tried the following:
map_hash_bucket_size 128;
map $uri $new {
/post http://b.com/post
# (repeated for all 100 posts)
}
And when I put the following line inside the server block:
rewrite ^ $new redirect;
It will redirect all 100 posts but all the other pages on my domain will error with: 302 Found.
Here is my whole server block inside the config:
server {
listen 80;
server_name b.com;
root /my/old/path/;
index index.html;
location = /favicon.ico {
log_not_found off;
access_log off;
}
location = /robots.txt {
allow all;
log_not_found off;
access_log off;
}
# Deny all attempts to access hidden files such as .htaccess, .htpasswd, .DS_Store (Mac).
location ~ /\. {
deny all;
access_log off;
log_not_found off;
}
location ~* \.(js|css|png|jpg|jpeg|gif|ico)$ {
expires max;
log_not_found off;
}
# example.com/index gets redirected to example.com/
location ~* ^(.*)/index$ {
return 301 $scheme://$host$1/;
}
# example.com/foo/ loads example.com/foo/index.html
location ~* ^(.*)/$ {
try_files $1/index.html #backend;
}
# example.com/a.html gets redirected to example.com/a
location ~* \.html$ {
rewrite ^(.+)\.html$ $scheme://$host$1 permanent;
}
# anything else not processed by the above rules:
# * example.com/a will load example.com/a.html
# * or if that fails, example.com/a/index.html
location / {
try_files $uri.html $uri/index.html $uri #backend;
}
# default handler
# * return error or redirect to base index.html page, etc.
location #backend {
try_files /404.html 404;
}
location ~ \.php$ {
expires off;
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
#NOTE: You should have "cgi.fix_pathinfo = 0;" in php.ini
include fastcgi_params;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_intercept_errors on;
fastcgi_pass php;
}
# apply the rewrite map
rewrite ^ $new redirect;
}
I am thinking the map interferes with the try_files call for location \ (which I need).
I found a solution for your problem on serverfault:
This is probably failing because you're trying to redirect all
requests, whether they matched something in the map or not.
To prevent this, check to see if there was a match first.
if ($new) {
return 301 $new;
}
So replace
rewrite ^ $new redirect;
with
if ($new) {
return 301 $new;
}
And you should be good to go
I have not been able to figure out how to do this with map, as it's still quite a string module to me. Now I've just put all the rewrites in a list inside my server block (above all location blocks), which is probably very slow. Now the file looks like this:
server {
listen 80;
server_name a.com;
root /my/old/path/;
index index.html;
rewrite /post http://b.com/post
# (x100)
location = /favicon.ico {
log_not_found off;
access_log off;
}
location = /robots.txt {
allow all;
log_not_found off;
access_log off;
}
# Deny all attempts to access hidden files such as .htaccess, .htpasswd, .DS_Store (Mac).
location ~ /\. {
deny all;
access_log off;
log_not_found off;
}
location ~* \.(js|css|png|jpg|jpeg|gif|ico)$ {
expires max;
log_not_found off;
}
# example.com/index gets redirected to example.com/
location ~* ^(.*)/index$ {
return 301 $scheme://$host$1/;
}
# example.com/foo/ loads example.com/foo/index.html
location ~* ^(.*)/$ {
try_files $1/index.html #backend;
}
# example.com/a.html gets redirected to example.com/a
location ~* \.html$ {
rewrite ^(.+)\.html$ $scheme://$host$1 permanent;
}
# anything else not processed by the above rules:
# * example.com/a will load example.com/a.html
# * or if that fails, example.com/a/index.html
location / {
try_files $uri.html $uri/index.html $uri #backend;
}
# default handler
# * return error or redirect to base index.html page, etc.
location #backend {
try_files /404.html 404;
}
location ~ \.php$ {
expires off;
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
#NOTE: You should have "cgi.fix_pathinfo = 0;" in php.ini
include fastcgi_params;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_intercept_errors on;
fastcgi_pass php;
}
# apply the rewrite map
rewrite ^ $new redirect;
}

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