.htaccess to Nginx rules - .htaccess

My website was working fine with Apache .htaccess rules. Now I have sifted it to nginx because ofa new server. I need help to convert following apache rules to nginx directives/configurations.
also, where in my ./etc/nginx/sites-enabled/thesite.com.conf should i pages the rules ?
RewriteEngine On
RewriteCond %{REQUEST_URI} ^(.+)\~s$
RewriteRule ^(.*) /stats.php?url=$1 [L]
RewriteCond %{REQUEST_URI} ^(.+)\~q$
RewriteRule ^(.*) /generate_qr.php?url=$1 [L]
RewriteCond %{REQUEST_URI} ^(.+)\~p$
RewriteRule ^(.*) /preview_url.php?url=$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*) /url_redirector.php?url=$1 [L]
Nginx Config
server {
listen 80;
listen [::]:80;
listen 443 ssl http2;
listen [::]:443 ssl http2;
ssl_certificate_key /etc/nginx/ssl-certificates/site.com.key;
ssl_certificate /etc/nginx/ssl-certificates/site.com.crt;
server_name site.com www1.site.com;
root /home/site/htdocs/site.com;
access_log /home/site/logs/nginx/access.log main;
error_log /home/site/logs/nginx/error.log;
if ($scheme != "https") {
rewrite ^ https://$host$uri permanent;
}
location ~ /.well-known {
auth_basic off;
allow all;
}
location / {
proxy_pass http://127.0.0.1:8080;
proxy_set_header Host $http_host;
proxy_set_header X-Forwarded-Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_hide_header X-Varnish;
proxy_redirect off;
proxy_max_temp_file_size 0;
proxy_connect_timeout 720;
proxy_send_timeout 720;
proxy_read_timeout 720;
proxy_buffer_size 128k;
proxy_buffers 4 256k;
proxy_busy_buffers_size 256k;
proxy_temp_file_write_size 256k;
}
location ~* ^.+\.(css|js|jpg|jpeg|gif|png|ico|gz|svg|svgz|ttf|otf|woff|woff2|eot|mp4|ogg|ogv|webm|webp|zip|swf|map)$ {
add_header Access-Control-Allow-Origin "*";
expires max;
access_log off;
}
if (-f $request_filename) {
break;
}
}
```

Related

Nginx server config URL rewrite to /backend and /frontend directories

I am trying to convert .htaccess file to a nginx server config.
There is default client side site which is served from - /frontend/public - this works fine.
There is admin CMS side site which is served from - /backend/public - this doesn't work
If I go to /cms then I get redirected to /cms/login which is correct. Nginx displays 404 error though which is not correct.
If I remove login requirement and go to /cms then it displays the web page as expected, but url changes from /cms to /backend/public.
I am not sure what I got wrong with the /cms and /backend/public in the nginx config.
nginx config:
server {
listen 80;
root /home/anon/web/project;
index index.php index.html index.htm index.nginx-debian.html;
server_name project.test;
location ~ ^/cms$ {
rewrite ^(.*)$ /cms/ redirect;
}
location /cms {
rewrite ^/cms(.*)$ /backend/public/$1 last;
}
location /backend/public {
if (!-e $request_filename) {
rewrite ^(.*)$ /index.php;
}
}
location / {
if (!-e $request_filename) {
rewrite ^/(.*)/$ /$1 redirect;
}
if (!-e $request_filename) {
rewrite ^(.+)$ /frontend/public/$1 break;
}
rewrite ^/[a-zA-Z0-9_/\-'"$]*$ /frontend/public/index.php last;
}
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
fastcgi_buffering off;
}
}
.htaccess file I am trying to convert into nginx config:
Options -Indexes
RewriteEngine on
RedirectMatch 301 ^/cms$ /cms/
RewriteRule ^cms(.*)$ backend/public/$1 [L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ /$1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{DOCUMENT_ROOT}/frontend/public/%{REQUEST_URI} -f
RewriteRule ^(.+)$ frontend/public/$1 [L]
RewriteRule ^[a-zA-Z0-9_/\-'"$]*$ frontend/public/index.php [L]

Few lines of Htaccess to Nginx Convert

I tried the online converters and none of them worked.
How can I convert the below lines of htaccess to nginx rule. I was only able to convert one line from the below code. I don't even know whether its right or wrong.
My nginx Conf
server {
server_name example.com;
root /var/www/example.com;
index index.html index.htm index.php;
location / {
try_files $uri $uri/ =404;
}
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
}
location ~ /\.ht {
deny all;
}
This is code I want to convert to nginx.
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews -Indexes
</IfModule>
RewriteEngine On
# Handle Authorization Header
RewriteCond %{HTTP:Authorization} .
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
# Redirect Trailing Slashes If Not A Folder...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} (.+)/$
RewriteRule ^ %1 [L,R=301]
# Send Requests To Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
# cors
Header set Access-Control-Allow-Origin "*"
</IfModule>
# Hide a specific file
<Files .env>
Order allow,deny
Deny from all
</Files>
These are the only lines I was able to convert.
# Send Requests To Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
To
try_files $uri $uri/ /index.php;
Still I have doubt in the above nginx rule whether it should contain ?$args before the colon mark or not?
Kindly help me with this.
Please note that I used an AI to assist in this conversion. Curious to see how it did.
# cors
add_header Access-Control-Allow-Origin *;
# gzip
gzip on;
gzip_disable "msie6";
gzip_vary on;
gzip_proxied any;
gzip_comp_level 6;
gzip_buffers 16 8k;
gzip_http_version 1.1;
gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript;
# cache
fastcgi_cache_path /var/cache/nginx levels=1:2 keys_zone=cache:10m max_size=10g inactive=60m;
fastcgi_cache_key "$scheme$request_method$host$request_uri";
fastcgi_cache_use_stale error timeout invalid_header http_500;
fastcgi_ignore_headers Cache-Control Expires Set-Cookie;
# security
add_header X-Frame-Options "SAMEORIGIN";
add_header X-XSS-Protection "1; mode=block";
add_header X-Content-Type-Options "nosniff";
add_header Referrer-Policy "no-referrer";
add_header X-Permitted-Cross-Domain-Policies "none";
# php
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;

No php extension and pretty url nginx config alternative

I just migrated my code from apache to nginx server.
What would be the alternative nginx config to my apache .htaccess.
What i use are rules for removing .php extension and pretty url rewrite.
RewriteEngine On
#remove .php
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
#for pretty url
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ index.php?url=$1 [NC,L]
Try this
map $uri $pretty_url {
~/(.*)$ $1;
}
server {
...
location / {
index index.php; # replace this to 'index index.php index.html index.htm;' if needed
try_files $uri $uri/ #extensionless-php;
}
location ~ \.php$ {
# default PHP-FPM handler here
...
}
location #extensionless-php {
if ( -f $document_root$uri.php ) {
rewrite ^ $uri.php last;
}
rewrite ^ /index.php?url=$pretty_url last;
}
}
Easy fix by changing one line. There's no need for extra blocks. You just need to change this one line in the /etc/nginx/nginx.conf or /etc/nginx/sites-available/your-site.com.
Change the location / directive try files to:
location / {
try_files $uri $uri/ $uri.html $uri.php$is_args$query_string;
}
Hope this works for you too :)

.htaccess RewriteRules to nginx.conf

I would like to convert this .htaccess file:
Options -MultiViews
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^(.+)$ index.php?url=$1 [QSA,L]
Into a nginx.conf file. So far my nginx.conf file looks like this:
server {
listen 80;
server_name _;
root /var/www/app;
index index.php;
location / {
try_files $uri /index.php;
}
location ~ \.php$ {
fastcgi_pass 127.0.0.1:8080;
fastcgi_index index.php;
include fastcgi_params;
}
}
I am getting a 502 bad gateway error.
Correct your location for compatibility with your apache rewrites:
location / {
try_files $uri /index.php?$args;
}
And error 502 means that fpm-php (fastcgi) backend is down, but this is not nginx issue :)

ToroPHP Router in nginx

I am hosting ToroPHP in /api/. My /api/.htaccess was
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond $1 !^(index\.php)
RewriteRule ^(.*)$ index.php/$1 [L]
This worked great. I converted this to the nginx equivalent which got me roughly this:
rewrite ^/api/(.*)$ /api/index.php/$1 last;
But this isn't working. What should the nginx equivalent be?
This question really belongs on http://serverfault.com, nonetheless:
Use try files
Rewriting isn't necessary, the desired results can be achieved more efficiently using try_files:
server {
...
location /api {
# try_files $uri $uri/ index.php$uri;
try_files $uri $uri/ index.php;
}
}
}
Note that it's not necessary to pass the url as an argument to index.php as it's already available as $_SERVER['REQUEST_URI'] (will require trivial modifications to index.php to work).
location /api {
try_files $uri $uri/ index.php$request_uri; # or /api/index.php$request_uri
}

Resources