Migrating from apache to nginx - apache

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

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.

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]

Convert .htaccess rule to nginx

I'm trying to move a site that was born on apache server to a nginx server.
I've the current .htaccess:
RewriteEngine on
RewriteRule ^(Personal)($|/) - [L]
RewriteRule ^mng/([-0-9a-zA-Z/%&]+)$ /index.php?aurl=$1 [L]
RewriteRule ^([-0-9a-zA-Z/%&]+)$ /index.php?url=$1 [L]
An online converter has converted these rules into:
location /mng {
rewrite ^/mng/([-0-9a-zA-Z/%&]+)$ /index.php?aurl=$1 break;
}
location / {
rewrite ^/([-0-9a-zA-Z/%&]+)$ /index.php?url=$1 break;
}
I added them to my nginx configuration but does not work, where am I wrong?
When I visit a page domain.ex/mng/index I can download the page..
This is my complete nginx conf:
server {
listen 80;
listen [::]:80;
root /var/www;
index index.php index.html;
server_name domain.ex;
location / {
rewrite ^/([-0-9a-zA-Z/%&]+)$ /index.php?url=$1 break;
}
location /mng {
rewrite ^/mng/([-0-9a-zA-Z/%&]+)$ /index.php?aurl=$1 break;
}
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/run/php/php7.0-fpm.sock;
}
}
After several attempts, I solved the problem with the following rules:
location /mng/ {
rewrite ^/(.*)/(.*)$ /?aurl=$1;
}
location / {
rewrite ^/(.*)/(.*)$ /?url=$1;
}

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;

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
}