Nginx rewrite not working on different locations - .htaccess

i have a problem that i can't figure it out,
so my problem is this, i am trying to locate diferent rewrite rules on a subfolder which is the mobile version. site.com is the main site and site.com/mobile/ is the mobile site, what i want to do is to add different rewrite to both locations, is this possible?
server {
listen 111.111.111:80;
server_name site.com www.site.com;
index index.php index.html index.htm;
root /home/admin/web/site.com/public_html;
access_log /var/log/nginx/domains/site.com.log combined;
access_log /var/log/nginx/domains/site.com.bytes bytes;
error_log /var/log/nginx/domains/site.com.error.log error;
location / {
rewrite ^/index.html$ / permanent;
rewrite ^/([^/]*)_([a-zA-Z0-9]+)/$ /watch.php?vid=$2;
rewrite ^/([^/]*)_([a-zA-Z0-9]+).html$ /watch.php?vid=$2;
rewrite ^/fpembed-(.*).swf$ /fpembed.php?vid=$1;
rewrite ^/uploads/thumbs/(.*)-social.(jpg|gif|png)$ /social-thumb.php?vid=$1;
rewrite ^/rss.xml$ /rss.php last;
location ~* ^.+\.(jpeg|jpg|png|gif|bmp|ico|svg|css|js)$ {
expires max;
}
location ~ [^/]\.php(/|$) {
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
if (!-f $document_root$fastcgi_script_name) {
return 404;
}
fastcgi_pass 127.0.0.1:9002;
fastcgi_index index.php;
include /etc/nginx/fastcgi_params;
}
}
location /mobile/ {
alias /home/admin/web/site.com/public_html/mobile/;
rewrite ^/index.html$ / permanent;
rewrite ^/category.html$ /category.php;
rewrite ^/([^/]*)_([a-zA-Z0-9]+).html$ /watch.php?vid=$2;
rewrite ^/fpembed-(.*).swf$ /fpembed.php?vid=$1;
rewrite ^/rss.xml$ /rss.php last;
location ~* ^.+\.(jpeg|jpg|png|gif|bmp|ico|svg|css|js)$ {
expires max;
}
location ~ [^/]\.php(/|$) {
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
if (!-f $document_root$fastcgi_script_name) {
return 404;
}
fastcgi_pass 127.0.0.1:9002;
fastcgi_index index.php;
include /etc/nginx/fastcgi_params;
}
}
can someoane tell me what i'm doing wrong here? Thanks a lot

Related

Change format from htaccess to nginx

I have apache server with htaccess file like this:
RewriteCond %{HTTP_HOST} ^.*$ [NC]
RewriteRule ^test/\$([^/]+)/([^/]+\.php)$ test/$2?VIRTUAL_DIRECTORY_NAME=$1 [L,QSA]
RewriteCond %{HTTP_HOST} ^.*$ [NC]
RewriteRule ^test/([^/]+)/(.*)$ $1/$2 [L]
And I am trying to convert it for nginx server.
I cant figure out how to transfor virtual directory to correct format.
This is my nginx config file:
server {
listen 80;
listen 443 ssl;
server_name admin.dev;
root "/home/vagrant/admin";
index index.html index.htm index.php;
charset utf-8;
rewrite_log on;
location /api/ {
rewrite ^/api/(.*)$ /api.php?$1 last;
}
location /res_partners/ {
rewrite ^res_partners/\$([^/]+)/([^/]+\.php)$ res_partners/$2?VIRTUAL_DIRECTORY_NAME=$1 last;
}
location = /favicon.ico { access_log off; log_not_found off; }
location = /robots.txt { access_log off; log_not_found off; }
access_log off;
error_log /var/log/nginx/admin.dev-error.log error;
sendfile off;
client_max_body_size 100m;
location ~ \.php$ {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_intercept_errors off;
fastcgi_buffer_size 16k;
fastcgi_buffers 4 16k;
fastcgi_connect_timeout 300;
fastcgi_send_timeout 300;
fastcgi_read_timeout 300;
}
location ~ /\.ht {
deny all;
}
ssl_certificate /etc/nginx/ssl/admin.dev.crt;
ssl_certificate_key /etc/nginx/ssl/admin.dev.key;
}
Any help?
Thanks
The first rule seems to rewrite /test/$something/somescript.php to /test/somescript.php?VIRTUAL_DIRECTORY_NAME=$something and these are literal $ characters. Your location ~ \.php$ { ... } container already matches any URI that ends with .php. So the first rule can be placed at the top of that container:
location ~ \.php$ {
rewrite ^/test/\$([^/]+)/([^/]+\.php)$ /test/$2?VIRTUAL_DIRECTORY_NAME=$1&$args break;
...
}
The second rule seems to rewrite /test/somedir/something to /somedir/something (where something is not a php script).
This can be achieved with a prefix location:
location /test/ {
rewrite ^/test/([^/]+)/(.*)$ /$1/$2;
}

Nginx Rewrite Not working

I have move my site to nginx but can get .htaccess roules right
below ius my code
server {
listen 80;
server_name mysite.com;
root /usr/share/nginx/mysite;
index index.php index.html index.htm;
location / {
rewrite ^/(.*)$ /members.php?id=$1 last;
try_files $uri $uri/ =404;
}
location ~ \.php$ {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
include fastcgi_params;
}
}
i want to display user url to be
mysite.com/jonny
instead of
mysite.com/memner.php?id=jonny
when i use
rewrite ^/(.*)$ /members.php?id=$1 last;
site don't load please help.
got fix for this
if (!-e $request_filename) {
rewrite /(.*) /members.php?id=$1 last ;
}

Nignx rewrite pattern on search query?

Its a very basic thing i need to do, but i cant figure this one out.
i have following rule in my nginx.conf
location ^/search {
#try_files index.php?search=$1 last;
rewrite ^/search/(.*)/ index.php?search=$1;
#rewrite ^/search/([^/]+)/page/([^/]+)/?$ /index.php?search=$1&page=$2;
}
location ^/article {
rewrite ^/article/([^/]+)/([^/]+)/$ /index.php?layout=article&id=$1;
}
location ^/images {
rewrite ^/images/([^/]+)$ /images/$1 break;
rewrite ^/images/([^/]+)/([^/]+)$ /images/$1/$2 break;
}
location ^/css {
rewrite ^/css/([^/]+)$ /css/$1 break;
rewrite ^/css/([^/]+)/([^/]+)$ /css/$1/$2 break;
}
location ~* ^/(css|img|js|flv|swf|download)/(.+)$ {
root $root_path;
}
location ~ /\.ht {
deny all;
}
location ~ \.php$ {
try_files $uri =404;
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 /etc/nginx/fastcgi_params;
}
basically all i want to do is
match below query on search to this above.
example.com/search-term1/
or
example.com/search term/
or
example.com/search/search-term1/
or
example.com/search/search-term1/page/1/
above url patterns ?how do i do this ? above rule gives me error 500 Internal server error.
in the Nginx log i get following as
2015/08/05 12:56:39 [error] 11136#0: *2 could not find named location "#rewrite", client: 202.172.107.194, server: example.com, request: "GET /search/search-term1/page/1/ HTTP/1.1", host: "example.com"
i fixed this issue by moving them out of the location block and putting them directly into the server block.

301 redirect is not removing the original paths in NGINX

I was trying to redirect a particular URL to a new URL using rewrite | 301 redirect catered by NGINX.
if ($request_uri = "/playlist/show/531a5aaefd3401705c000a32") {
rewrite ^.*$ playlist/all/ redirect;
}
If my request URI is http://servername.com/playlist/show/531a5aaefd3401705c000a32, then it should redirect to http://servername.com/playlist/all/
But what's happening right now is that it the original paths from old URL are still there:
http://servername.com/playlist/show/playlist/all/
The /playlist/show/ slashes/paths are still there, so it's redirecting to the wrong URI.
I'm pretty new in handling NGINX. Please bear with me. Thanks!
EDITS:
Here's my conf to be clearer:
server {
listen 80;
server_name site.servername.com;
server_tokens off;
charset utf-8;
root /home/site/stm/public;
location / {
index index.php;
# auth_basic "Restricted";
# auth_basic_user_file /home/site/stm/.htpasswd;
}
location = /favicon.ico {
access_log off;
log_not_found off;
empty_gif;
expires 30m;
}
if ($request_uri = "/playlist/show/531a5aaefd3401705c000a32") {
rewrite ^.*$ playlist/all/ redirect;
}
if ($request_uri = "/favicon.ico") {
rewrite ^.*$ /favicon.ico last;
}
if ($request_uri !~* ^/(?:static|favicon.ico)) {
rewrite ^.*$ /index.php last;
}
location ~ \.php$ {
include /etc/nginx/fastcgi_params;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
}
You're doing it wrong: http://wiki.nginx.org/Pitfalls
You should never use if ($uri or if ($request_uri because it's just a very bad method of making location.
location =/playlist/show/531a5aaefd3401705c000a32 {
return 302 /playlist/all/;
}
Simple, fast, no regexps.
Reference to study:
http://nginx.org/r/location
Converting rewrite rules
How nginx processes a request

FastCGI - Rewrite - Location with Nginx

I just switched to nginx but I'm having trouble with my URL-Rewriting
I used
location /id/ {
rewrite ^/id/(.*) /index.php?id=$1 break;
}
But the php code is not interpreted, worst it's downloaded raw.
Yet .php files are configured as follow:
location ~ \.php$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /var/www/my_app$fastcgi_script_name;
include /etc/nginx/fastcgi_params;
}
What is wrong with my vhost ?
EDIT: Here is the entire vhost
server {
listen 80; ## listen for ipv4
server_name viditx.com www.viditx.com; ## change this to your own domain name
# I find it really useful for each domain & subdomain to have
# its own error and access log
error_log /var/log/nginx/viditx.com.error.log;
access_log /var/log/nginx/viditx.com.access.log;
root /var/www/viditx;
location / {
# Change this to the folder where you want to store your website
index index.html index.htm index.php;
}
location /phpmyadmin {
root /usr/share/;
index index.php index.html index.htm;
location ~ ^/phpmyadmin/(.+\.php)$ {
try_files $uri =404;
root /usr/share/;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include /etc/nginx/fastcgi_params;
}
location ~* ^/phpmyadmin/(.+\.(jpg|jpeg|gif|css|png|js|ico|html|xml|txt))$ {
root /usr/share/;
}
}
location /phpMyAdmin {
rewrite ^/* /phpmyadmin last;
}
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /var/www/nginx-default;
}
location ~ \.php$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
# again, change the directory here to your website's root directory
# make sure to leave $fastcgi_script_name; on the end!
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include /etc/nginx/fastcgi_params;
}
}
Did you read the documentation?
break -> last:
location /id/ {
rewrite ^/id/(.*)$ /index.php?id=$1 last;
}
You should try :
location /id/ {
rewrite '^/id/(.*)$' /index.php?id=$1 break;
}
and :
location ~ \.php$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include /etc/nginx/fastcgi_params;
}
Have fun !

Resources