htaccess to nginx configuration - apache

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

Related

NGINX handling $_GET query parameters differently to Apache

I'm currently migrating a website from an Apache build that contains the following .htaccess rules:
<ifmodule mod_rewrite.c>
RewriteEngine on
RewriteBase /
RewriteRule ^$ index.php [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond $1 !^(index\.php|admin|captcha|print|pdf\.php|robots\.txt)
RewriteRule (.*) index.php?req=$1 [QSA,L]
</ifmodule>
I've setup NGINX and added the following location block:
location / {
try_files $uri /index.php?req=$uri$is_args$args;
}
This works for most pages, but some internal pages that handle query string are failing. After some debugging, I found that dumping $_GET was returning differently. The Apache server is returning:
array(2) { ["req"]=> string(8) "checkout" ["quick"]=> string(23) "email#address.com" }
which works perfectly for this old website, but the NGINX server is returning:
array(1) { ["req"]=> string(39) "/checkout?quick=email#address.com" }
Can anybody help me understand what's going on? It seems there's an issue with me passing req, as well as additional query params.
Update: I've managed to partly resolve this with the following NGINX settings, but it's annoying I have to manually exclude every path I don't want to be rewritten:
location / {
try_files $uri $uri/ /index.php?$is_args$args;
rewrite ^/(.*)$ /index.php?req=$1 last;
}
location /admin {
try_files $uri $uri/ /admin/index.php?$is_args$args;
}
location /js/ {
# Do nothing. nginx will serve files as usual.
}
location /css/ {
# Do nothing. nginx will serve files as usual.
}
location /images/ {
# Do nothing. nginx will serve files as usual.
}
I managed to resolve my issue with the following:
location / {
try_files $uri $uri/ /index.php?$is_args$args;
if (!-e $request_filename) {
rewrite ^/(.*)$ /index.php?req=$1 last;
}
}
which should prevent rewriting of non-request filenames.

mod rewrite from apache to nginx

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

Magento Different Behave in Nginx

One server same source code Magento Enterprise , if i run it in Apache after post request one Get request received , but in nginx not work , and shows me page not found 404.
I check it using firebug in Firefox .
URL : /customer/account/create/
After click Subbmit it redirect to
URL : /customer/account/index/ = and user longed page come
But in Nginx
URL : /customer/account/create/
After click Subbmit it redirect to
http://52.88.205.17/customer/account/createpost/ = 404 page not found
Apache Server default configuration and htaccess is
Options +FollowSymLinks
RewriteEngine on
RewriteRule ^api/rest api.php?type=rest [QSA,L]
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
RewriteCond %{REQUEST_METHOD} ^TRAC[EK]
RewriteRule .* - [L,R=405]
RewriteCond %{REQUEST_URI} !^/(media|skin|js)/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule .* index.php [L]
And Nginx Configuration is
server {
listen 80;
server_name http://52.88.205.17;
location / {
root /var/www/html/src/;
index index.php;
try_files $uri $uri/ #handler;
}
location #handler { ## Magento uses a common front handler
rewrite / /index.php;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /var/www/html/src/;
}
#location /index.php/admin { deny all;}
#Disable .htaccess and Hidden Files
location /. { return 404; }
location ~ .php/ { ## Forward paths like /js/index.php/x.js to relevant handler
rewrite ^(.*.php)/ $1 last; }
location ~ \.php$ {
root /var/www/html/src/;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
I solved this problem Increasing PHP Buffer Size
fastcgi_buffers 16 16k;
fastcgi_buffer_size 32k;

Apache rewrite rule to NGINX for HHVM

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

I need to translate my htaccess for nginx to work

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
}