NGINX handling $_GET query parameters differently to Apache - 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.

Related

Convert nginx conf to .htaccess file

I have set up the existing Yii2 advanced project in Apache server. before it was on nginx server. I have a nginx.conf file. that I want to convert it .htaccess file to make a project working on Apache server as well.
Here is the nginx.conf file code
location / {
index index.php index.html;
try_files $uri $uri/ /index.php?$args;
} # / location
location /html {
alias /../../html/;
}
location /api {
if ( !-e $request_filename ) {
rewrite ^/api/(.*)$ /api/index.php?$1 last;
}
}
location /dten {
if ( !-e $request_filename ) {
rewrite ^/dten/(.*)$ /dten/index.php?$1 last;
}
}
location /dten-backend {
if ( !-e $request_filename ) {
rewrite ^/dten-backend/(.*)$ /dten-backend/index.php?$1 last;
}
}
location /dten-crm {
if ( !-e $request_filename ) {
rewrite ^/dten-crm/(.*)$ /dten-crm/index.php?$1 last;
}
}
I tried this, but it is not working.
# if a directory or a file exists, use it directly
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# otherwise forward it to index.php
RewriteRule / index.php
# Handle the case of backend, skip ([S=1]) the following rule, if current matcheds
RewriteRule ^/dten-backend(/(.*))?$ /dten-backend/$2 [S=1]

htaccess rewrite to nginx config not working

I'm having a problem converting an appache .htaccess file work with nginx.
The .htaccess file
<ifModule mod_rewrite.c>
Allow from 127.0.0.1
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^index(|/)$ index.php
RewriteRule ^logout(|/)$ logout.php
RewriteRule ^keeping/([^/.]+)(|/)$ keeping.php?s=$1
</ifModule>
My Conversion:
location / {
if (!-e $request_filename){
rewrite ^/index(|/)$ /index.php;
}
}
location /logout {
rewrite ^/logout(|/)$ /logout.php;
}
location /kyhsadminpanel {
rewrite ^/keeping/([^/.]+)(|/)$ /keeping.php?s=$1;
}
doesn't work.
It always shows "404 not found" for url's such as /kyhsadminpanel/keeping/index
and /kyhsadminpanel/keeping/news...
You might want to note you used: https://winginx.com/en/htaccess
I tend to use: https://timmehosting.de/htaccess-converter
Here is the result from them.
if (!-d $request_filename){
set $rule_0 1$rule_0;
}
if (!-f $request_filename){
set $rule_0 2$rule_0;
}
if ($rule_0 = "21"){
rewrite ^/index(|/)$ /index.php ;
}
rewrite ^/logout(|/)$ /logout.php ;
rewrite ^/keeping/([^/.]+)(|/)$ /keeping.php?s=$1 ;

How to convert .htaccess to a nginx equivalent?

How to convert .htaccess configuration to nginx?
My .htacess:
Options -MultiViews
RewriteEngine On
RewriteBase /mvc/public
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.+)$ index.php?url=$1 [QSA,L]
I tried this:
nginx configuration
location /mvc/public/ {
if (!-e $request_filename){
rewrite ^/mvc/public/(.+)$ /mvc/public/index.php?url=$1 [QSA,L];
}
}
But this did not work!
Could someone help me?
The [QSA,L] is not nginx syntax - see this document for details.
location /mvc/public/ {
if (!-e $request_filename) {
rewrite ^/mvc/public/(.+)$ /mvc/public/index.php?url=$1 last;
}
}
Similar behaviour can be accomplished with try_files rather than the if:
location /mvc/public/ {
try_files $uri $uri/ #rewrite;
}
location #rewrite {
rewrite ^/mvc/public/(.+)$ /mvc/public/index.php?url=$1 last;
}
See this document for details.

Migrating from apache to nginx

So I've been using apache for all of my PHP projects, but now I've been forced to using nginx on the server. I've no clue how to do pretty urls with nginx. So I started converting my .htaccess file into an nginx config script, and tried modifying the nginx config file, but no luck.
This is my .htaccess file:
Options -MultiViews
RewriteEngine On
RewriteBase /wallfly-mvc/public
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.+)$ index.php?url=$1 [QSA,L]
This is my nginx.conf server block:
server {
listen 80; listen [::]:80;
server_name deco3801-superflyz.uqcloud.net;
root /var/www/htdocs;
include "set_cookie.conf";
rewrite_by_lua_file "etc/nginx/lua/auth_filter.lua";
location / {
index index.html index.htm index.php index.jsp index.aspx;
try_files $uri $uri/ =404;
}
location /wallfly-mvc/public/ {
if (!-e $request_filename) {
rewrite ^/wallfly-mvc/public/(.+)$ /wallfly-mvc/public/index.php?url=$1 break;
}
}
}
I want every request to point to public/index.php. This is my project structure:
wallfly-mvc/
app/
public/
index.php
css/
js/
img/
How should I change the nginx.conf file? Any help would be appreciated.
You can use location ^~ to capture the directory. Its priority is higher location /
server {
#...
location ^~ /wallfly-mvc/public/ {
if (!-e $request_filename) {
rewrite ^/wallfly-mvc/public/(.+)$ /wallfly-mvc/public/index.php?url=$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;