Nginx configuration file - .htaccess

I'm quite new to Nginx and I'm testing out if this is the way to go for me in the future. On my server there are a couple of websites. I managed to set up Nginx correctly. However, on the old setup there was a .htaccess file, which made sure all the urls worked correctly.
This is the .htaccess file:
RewriteEngine on
RewriteBase /
RewriteRule ^verzekeringen/([a-z]+)$ products/product/index.php?page=$1 [L]
RewriteRule ^hypotheken/([a-z]+)$ products/product/index.php?page=$1 [L]
RewriteRule ^verzekeringen$ products/index.php?type=1 [L]
RewriteRule ^hypotheken$ products/index.php?type=2 [L]
An online converter, converted this to:
# nginx configuration
location /verzekeringen {
rewrite ^/verzekeringen/([a-z]+)$ /products/product/index.php?page=$1 break;
}
location /hypotheken {
rewrite ^/hypotheken/([a-z]+)$ /products/product/index.php?page=$1 break;
}
location = /verzekeringen {
rewrite ^(.*)$ /products/index.php?type=1 break;
}
location = /hypotheken {
rewrite ^(.*)$ /products/index.php?type=2 break;
}
In the standard configuration file, there is this part, where I think it should be. The problem however, is that I don't really know how to incorporate this into the file.
server {
server_name testwebsite.nl www.testwebsite.nl;
root /var/www/testwebsite.nl/htdocs;
location ~ \.php$ {
root /var/www/testwebsite.nl/htdocs;
fastcgi_pass ***.*.*.*:****;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}

You can actually paste the generated rules as is anywhere before ~ \.php$ rule.
you can also try this folded also :
server {
server_name testwebsite.nl www.testwebsite.nl;
root /var/www/testwebsite.nl/htdocs;
location /verzekeringen {
rewrite ^/verzekeringen/([a-z]+)$ /products/product/index.php?page=$1 break;
}
location /hypotheken {
rewrite ^/hypotheken/([a-z]+)$ /products/product/index.php?page=$1 break;
}
location = /verzekeringen {
rewrite ^(.*)$ /products/index.php?type=1 break;
}
location = /hypotheken {
rewrite ^(.*)$ /products/index.php?type=2 break;
}
location ~ \.php$ {
root /var/www/testwebsite.nl/htdocs;
fastcgi_pass ***.*.*.*:****;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}

See directive location and rewrite.
location ~ ^/verzekeringen$ {
rewrite ^(.*)$ /products/index.php?type=2 last;
}
location ~ ^/hypotheken$ {
rewrite ^(.*)$ /products/index.php?type=1 last;
}
location ~ /verzekeringen/[a-z]+$ {
rewrite ^/verzekeringen/(.*)$ /products/product/index.php?page=$1 last;
}
location ~ /hypotheken/[a-z]+$ {
rewrite ^/hypotheken/([a-z]+)$ /products/product/index.php?page=$1 last;
}

Related

Nginx rewrite not working on different locations

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

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

Resources