I'm having some trouble in transformation of htaccess from apache to nginx.
actual htaccess rule:
options +followsymlinks
RewriteEngine On
RewriteCond $1 !^(images|system|themes|css|js|flex|slider|rss|favicon\.ico|robots\.txt|index\.php) [NC]
RewriteRule ^(.*) /index.php?$1 [L]
this rule set is used in expression engine 1.7
my current solution :
location / {
index index.php;
try_files $uri $uri/ #ee;
}
location #ee {
rewrite ^(.*) /index.php?/$1 last;
}
location /index.php {
include fastcgi_params;
set $script $uri;
set $path_info $uri;
if ($args ~* "^(/.+)$") {
set $path_info $1;
}
fastcgi_pass fastcgi-upstream_${server_name};
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PATH_INFO $path_info;
}
location ~* \.php$ {
include fastcgi_params;
fastcgi_pass fastcgi-upstream_${server_name};
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_script_name;
}
but it not reflexting 100% the same thing as in htaccess.
You can use the location block in Nginx. This will redirect all your request to index.php.
location / {
rewrite ^/(.*) /index.php?$1 break;
}
Related
I have two htaccess files one for a website (root folder) and one for its admin panel (admin folder):
root
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
RewriteRule ^admin/(.*)$ admin/$1 [NC,L,QSA]
RewriteRule ^sign-out$ index.php?act=auth-signOut [NC,L,QSA]
RewriteRule ^get/(.*)$ index.php?pag=download&id=$1 [NC,L,QSA]
RewriteRule ^g/(.*)$ callback.php?act=download-download&id=$1 [NC,L,QSA]
RewriteRule ^activate/(.*)$ index.php?act=auth-activate&u=$1 [NC,L,QSA]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?pag=cms&title=$1 [NC,L,QSA]
/admin
RewriteRule ^sign-in$ views/login.html [NC,L,QSA]
RewriteRule ^sign-out$ index.php?act=auth-logout [NC,L,QSA]
I was able to convert the website part to nginx config rules but when I add the admin section it doesn't work. Any help is appreciated.
server {
listen 80;
server_name fileorbs.com;
root /var/www/fileorbs/public_html;
index index.php index.html;
location ~ \.php$ {
try_files $uri /index.php =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 fastcgi_params;
}
location / {
# First attempt to serve request as file, then
# as directory, then fall back to displaying a 404.
if (!-e $request_filename){
rewrite ^/(.*)$ /index.php?pag=cms&title=$1&code=$2;
}
rewrite ^/admin/(.*)$ /admin/$1 last;
rewrite ^/sign-out$ /index.php?act=auth-signOut last;
rewrite ^/get/(.*)$ /index.php?pag=download&id=$1 last;
rewrite ^/g/(.*)$ /index.php?act=download-download&id=$1 last;
rewrite ^/activate/(.*)$ /index.php?act=auth-activate&u=$1 last;
try_files $uri $uri/ =404;
}
location /admin {
rewrite ^/sign-in$ /views/login.html last;
rewrite ^/sign-out$ /index.php?act=auth-logout last;
}
}
Note, that apache first checks root-.htaccess, then goes to /admin.
Nginx uses location as preferred match. These are two totally different methods.
http://nginx.org/en/docs/http/request_processing.html
Adding last to every rewrite you tell nginx "perform that rewrite", so in fact, your /admin location is not processes anywhere now.
http://nginx.org/ru/docs/http/ngx_http_rewrite_module.html#rewrite
Also, what exactly should that rule do? As I undestand, it just redirects to itself one time:
RewriteRule ^admin/(.*)$ admin/$1 [NC,L,QSA]
rewrite ^/admin/(.*)$ /admin/$1 last;
If I understanded your config right, something like that should work:
server {
listen 80;
server_name fileorbs.com;
root /var/www/fileorbs/public_html;
index index.php index.html;
location ~ \.php$ {
try_files $uri /index.php =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 fastcgi_params;
}
location / {
# First attempt to serve request as file, then
# as directory, then fall back to displaying a 404.
if (!-e $request_filename){
rewrite ^/(.*)$ /index.php?pag=cms&title=$1&code=$2;
}
rewrite ^/sign-out$ /index.php?act=auth-signOut last;
rewrite ^/get/(.*)$ /index.php?pag=download&id=$1 last;
rewrite ^/g/(.*)$ /index.php?act=download-download&id=$1 last;
rewrite ^/activate/(.*)$ /index.php?act=auth-activate&u=$1 last;
try_files $uri $uri/ =404;
}
location /admin {
rewrite ^/admin/(.*)$ /admin/$1 break;
rewrite ^/sign-in$ /views/login.html last;
rewrite ^/sign-out$ /index.php?act=auth-logout last;
try_files $uri $uri/ =404;
}
}
I have got this config file without any rewrite
server {
listen 80;
listen [::]:80;
root /var/www/testhop.com/www;
index index.php index.html index.htm;
server_name testhop.com;
location / {
try_files $uri $uri/ =404;
}
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;
}
}
I have found some code for making rewriting:
try_files $uri $uri/ /index.php?q=$uri&$args;
I have modified it to final form of:
server {
(...)
location / {
try_files $uri $uri/ /index.php/$uri&$args;
}
location ~ \.php$ {
(...)
}
But this configuration gives me the 500 error.
My original apache rewrite is:
RewriteRule ^(.*)$ /index.php/$1 [L]
How make it works in format index.php/url-params
?
Your location ~ \.php$ block accepts URIs that end with .php (not including the query string). By adding a / after the .php you are appending path info.
Using the documentation this should work for you:
location ~ [^/]\.php(/|$) {
fastcgi_split_path_info ^(.+?\.php)(/.*)$;
if (!-f $document_root$fastcgi_script_name) {
return 404;
}
include fastcgi_params;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param HTTP_PROXY "";
}
The location regex has been changed to accept URIs with path info. The try_files directive has been replaced by an evil if, as the URI needs to be split first, before being tested for file existence.
I like to place the include statement before any fastcgi_param statement to avoid the latter being silently overridden.
I am struggling with setting up rewrite rules for prestashop with the following setup
Prestashop 1.6
SSL
Multiligual Site (fr + en in my case)
Nginx 1.4.1 (that is important for ssl settings I found)
I have used some of the references below
http://www.prestashop.com/forums/topic/323391-another-nginx-ssl-rewrite-rules-problem/
http://www.prestashop.com/forums/topic/321261-seo-friendly-nginx-rewrites/ (not multilingual)
PRESTASHOP NGINX + REWRITE RULES
and got to this config below.
Everything works except some of my payment modules which return a url like
mysite.com/en/index.php?parameter1=1¶meter2=2
This triggers a 404
It looks like the url should be rewritten as
mysite.com/index.php?parameter1=1¶meter2=2
I have two questions:
Which rule is handling urls like mysite.com/en/16-crews which are working fine?
and what is it translated to ? (I am just curious to understand how that works)
How do I set up a rule to rewrite
mysite.com/en/index.php?parameter1=1¶meter2=2
to
mysite.com/index.php?parameter1=1¶meter2=2
It must also work with the french side of the site /fr/index.php to /index.php
server {
listen 80;
listen 443 ssl;
server_name mysite.com www.mysite.com;
ssl on;
ssl_certificate /etc/nginx/ssl/mysite.crt;
ssl_certificate_key /etc/nginx/ssl/mysite.key;
access_log /var/log/nginx/mysite.access.log;
error_log /var/log/nginx/mysite.error.log;
rewrite_log on;
location / {
root /srv/d_h2osensations/www/www.mysite.com/htdocs;
index index.html index.htm index.php;
rewrite ^/api/?(.*)$ /webservice/dispatcher.php?url=$1 last;
rewrite ^/([0-9])(-[_a-zA-Z0-9-]*)?/[_a-zA-Z0-9-]*.jpg$ /img/p/$1/$1$2.jpg last;
rewrite ^/([0-9])([0-9])(-[_a-zA-Z0-9-]*)?/[_a-zA-Z0-9-]*.jpg$ /img/p/$1/$2/$1$2$3.jpg last;
rewrite ^/([0-9])([0-9])([0-9])(-[_a-zA-Z0-9-]*)?/[_a-zA-Z0-9-]*.jpg$ /img/p/$1/$2/$3/$1$2$3$4.jpg last;
rewrite ^/([0-9])([0-9])([0-9])([0-9])(-[_a-zA-Z0-9-]*)?/[_a-zA-Z0-9-]*.jpg$ /img/p/$1/$2/$3/$4/$1$2$3$4$5.jpg last;
rewrite ^/([0-9])([0-9])([0-9])([0-9])([0-9])(-[_a-zA-Z0-9-]*)?/[_a-zA-Z0-9-]*.jpg$ /img/p/$1/$2/$3/$4/$5/$1$2$3$4$5$6.jpg last;
rewrite ^/([0-9])([0-9])([0-9])([0-9])([0-9])([0-9])(-[_a-zA-Z0-9-]*)?/[_a-zA-Z0-9-]*.jpg$ - img/p/$1/$2/$3/$4/$5/$6/$1$2$3$4$5$6$7.jpg last;
rewrite ^/([0-9])([0-9])([0-9])([0-9])([0-9])([0-9])([0-9])(-[_a-zA-Z0-9-]*)?/[_a-zA-Z0-9-]*.jpg$ /img/p/$1/$2/$3/$4/$5/$6/$7/$1$2$3$4$5$6$7$8.jpg last;
rewrite ^/([0-9])([0-9])([0-9])([0-9])([0-9])([0-9])([0-9])([0-9])(-[_a-zA-Z0-9-]*)?/[_a-zA-Z0-9-]*.jpg$ /img/p/$1/$2/$3/$4/$5/$6/$7/$8/$1$2$3$4$5$6$7$8$9.jpg last;
rewrite ^/c/([0-9]+)(-[_a-zA-Z0-9-]*)/[_a-zA-Z0-9-]*.jpg$ /img/c/$1$2.jpg last;
rewrite ^/c/([a-zA-Z-]+)/[a-zA-Z0-9-]+.jpg$ /img/c/$1.jpg last;
rewrite ^/([0-9]+)(-[_a-zA-Z0-9-]*)/[_a-zA-Z0-9-]*.jpg$ /img/c/$1$2.jpg last;
try_files $uri $uri/ /index.php?$args;
}
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 /srv/d_h2osensations/www/www.mysite.com/htdocs$fastcgi_script_name;
}
# Deny access to .htaccess
location ~ /\.ht {
deny all;
}
location /phpmyadmin
{ root /usr/share/;
index index.php index.html index.htm;
location ~ ^/phpmyadmin/(.+\.php)$ {
try_files $uri =404;
root /usr/share/;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
location ~* ^/phpmyadmin/(.+\.(jpg|jpeg|gif|css|png|js|ico|html|xml|txt))$ {
root /usr/share/;
}
}
location /phpMyAdmin {
rewrite ^/* /phpmyadmin last;
}
}
Thanks in advance.
Nic
After a few weeks of looking around this is what works for me
server {
listen 80;
#listen [::]:80 default_server ipv6only=on;
listen 443 default ssl;
#ssl on;
ssl_certificate /etc/nginx/ssl/cert.crt;
ssl_certificate_key /etc/nginx/ssl/cert-key.key;
access_log /var/log/nginx/h2o.prod.access.log;
error_log /var/log/nginx/h2o.prod.error.log;
root /var/www/www.mysite.com/htdocs;
#root /usr/share/nginx/html;
index index.html index.htm index.php;
# Make site accessible from http://localhost/
server_name mysite.com www.mysite.com;
#Specify a charset
charset utf-8;
rewrite ^/([0-9])(\-[_a-zA-Z0-9-]*)?(-[0-9]+)?/.+\.jpg$ /img/p/$1/$1$2$3.jpg last;
rewrite ^/([0-9])([0-9])(\-[_a-zA-Z0-9-]*)?(-[0-9]+)?/.+\.jpg$ /img/p/$1/$2/$1$2$3$4.jpg last;
rewrite ^/([0-9])([0-9])([0-9])(\-[_a-zA-Z0-9-]*)?(-[0-9]+)?/.+\.jpg$ /img/p/$1/$2/$3/$1$2$3$4$5.jpg last;
rewrite ^/([0-9])([0-9])([0-9])([0-9])(\-[_a-zA-Z0-9-]*)?(-[0-9]+)?/.+\.jpg$ /img/p/$1/$2/$3/$4/$1$2$3$4$5$6.jpg last;
rewrite ^/([0-9])([0-9])([0-9])([0-9])([0-9])(\-[_a-zA-Z0-9-]*)?(-[0-9]+)?/.+\.jpg$ /img/p/$1/$2/$3/$4/$5/$1$2$3$4$5$6$7.jpg last;
rewrite ^/([0-9])([0-9])([0-9])([0-9])([0-9])([0-9])(\-[_a-zA-Z0-9-]*)?(-[0-9]+)?/.+\.jpg$ /img/p/$1/$2/$3/$4/$5/$6/$1$2$3$4$5$6$7$8.jpg last;
rewrite ^/([0-9])([0-9])([0-9])([0-9])([0-9])([0-9])([0-9])(\-[_a-zA-Z0-9-]*)?(-[0-9]+)?/.+\.jpg$ /img/p/$1/$2/$3/$4/$5/$6/$7/$1$2$3$4$5$6$7$8$9.jpg last;
rewrite ^/([0-9])([0-9])([0-9])([0-9])([0-9])([0-9])([0-9])([0-9])(\-[_a-zA-Z0-9-]*)?(-[0-9]+)?/.+\.jpg$ /img/p/$1/$2/$3/$4/$5/$6/$7/$8/$1$2$3$4$5$6$7$8$9$10.jpg last;
rewrite ^/c/([0-9]+)(-[_a-zA-Z0-9-]*)/[_a-zA-Z0-9-]*.jpg$ /img/c/$1$2.jpg last;
rewrite ^/c/([a-zA-Z-]+)/[a-zA-Z0-9-]+.jpg$ /img/c/$1.jpg last;
rewrite ^/([0-9]+)(-[_a-zA-Z0-9-]*)/[_a-zA-Z0-9-]*.jpg$ /img/c/$1$2.jpg last;
rewrite ^/order$ /index.php?controller=order last;
if (!-e $request_filename){
rewrite ^(.*)$ /index.php last;
}
# Redirect needed to "hide" index.php
location / {
try_files $uri $uri/ /index.php?q=$uri&$args;
}
location ~ \.php$ {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
include fastcgi_params;
}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
location ~ /\.ht {
deny all;
}
}
We're moving from apache to NGINX and Hip Hop Virtual Machine (HHVM). I can't seem to get our rewrite rules lined up and working properly in NGINX. Here is the current working apache rule set:
RewriteEngine On
RewriteBase /
#send www.domain.com -> domain.com
RewriteCond %{HTTP_HOST} ^www\.domain\.com$
RewriteRule ^(.*)$ http://domain.com%{REQUEST_URI} [R=301,L]
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteRule td/api(.*) http://dp.domain.com/api$1 [P,L]
RewriteRule ^.*$ /index.php [NC,L]
</IfModule>
Here is what I have tried currently:
server {
server_name test.domain.com;
listen 80;
root /path/public_html;
access_log /var/log/nginx/access.log;
error_log /var/log/nginx/error.log;
index index.html index.htm index.php;
location / {
try_files $uri $uri/ /index.php$is_args$args;
}
location ~ \.php$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_param SCRIPT_FILENAME /path/public_html/index.php;
include fastcgi_params;
}
}
What exactly is the problem you're encountering and which version of ZF are you using? The following config works for ZF1.12. You can test the config and debug it by calling the following command (on RHEL/CentOS):
$ service nginx configtest
You can also check the error logs:
$ tail -f /var/log/nginx/error.log
The config:
server {
listen 80 default;
server_name test.domain.com;
root /path/public_html;
index index.php index.html index.htm;
client_max_body_size 3M;
location / {
server_tokens off;
try_files $uri $uri/ /index.php?$args;
}
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;
}
location ~* \.(jpg|jpeg|gif|png|css|js|ico|xml)$ {
access_log off;
log_not_found off;
expires 360d;
}
location ~ /\.ht {
deny all;
}
}
well I have this mod_rewrite in my old apache configuration (htaccess) and I am trying to set up the configuration to nginx, but I can not really understand how it really works in nginx, I'm a newbie in all this nginx stuff, could anyone help me to translate this... Thank you so much in advance.
Current htacces file:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !^(/static/)
RewriteRule . /app.php [L]
</IfModule>
Virtual config file test.url:
server {
server_name test.url;
access_log /var/www/test.url/logs/access.log;
error_log /var/www/test.url/logs/error.log;
root /var/www/test.url/public_html/blank;
location / {
app.php;
}
location ~ \.php$ {
include /etc/nginx/fastcgi_params;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /var/www/test.url/public_html/$
}
}
Instead of:
location / {
app.php;
}
Try:
location /static/ {
# do nothing, we don't want to rewrite /static/
}
location / {
try_files $uri $uri/ /app.php
}