Can't convert .htaccess to nginx directives - php

in order to install Taskly crm, I'm trying to convert three simple directives by an .htaccess file to nginx vhosts directives without success.
I've installed Cloudpanel on my own vps server and when i add a website it comes with pre-configured vhosts, and this is the default file i have:
server {
listen 8080;
listen [::]:8080;
server_name crm.mydomain.it;
{{root}}
try_files $uri $uri/ /index.php?$args;
index index.php index.html;
location ~ \.php$ {
include fastcgi_params;
fastcgi_intercept_errors on;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
try_files $uri =404;
fastcgi_read_timeout 3600;
fastcgi_send_timeout 3600;
fastcgi_param HTTPS "on";
fastcgi_param SERVER_PORT 443;
fastcgi_pass 127.0.0.1:{{php_fpm_port}};
fastcgi_param PHP_VALUE "{{php_settings}}";
}
if (-f $request_filename) {
break;
}
}
server {
listen 80;
listen [::]:80;
listen 443 ssl http2;
listen [::]:443 ssl http2;
{{ssl_certificate_key}}
{{ssl_certificate}}
server_name crm.mydomain.it;
{{root}}
{{nginx_access_log}}
{{nginx_error_log}}
if ($scheme != "https") {
rewrite ^ https://$host$uri permanent;
}
location ~ /.well-known {
auth_basic off;
allow all;
}
{{settings}}
location / {
{{varnish_proxy_pass}}
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;
}
}
And i have those .htaccess rules to add:
<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_URI} !(\.css|\.js|\.png|\.jpg|\.jpeg|\.gif|robots\.txt|\.ico|\.woff|\.woff2|.ttf|\.svg)$ [NC]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !^/public/
RewriteRule ^(css|assets|landing|storage|installer|js|custom)/(.*)$ public/$1/$2 [L,NC]
</IfModule>
I translated those directives into this code with an online tool, and i tried to insert those lines where i supposed i should with success, as Cloudpanel if there is a error will not allow me to save the vhosts file, but it allowed me without problem. The problem is that adding those lines make my domain cause a loop redirect error.
Translated directives:
# nginx configuration by winginx.com
autoindex off;
location ~* (\.css|\.js|\.png|\.jpg|\.jpeg|\.gif|robots\.txt|\.ico|\.woff|\.woff2|.ttf|\.svg)$ {
}
location ~ /public/ {
}
location / {
if (!-e $request_filename){
rewrite ^(.*)$ /%1 redirect;
}
if (!-e $request_filename){
rewrite ^(.*)$ /index.php break;
}
if (!-e $request_filename){
rewrite ^/(css|assets|landing|storage|installer|js|custom)/(.*)$ /public/$1/$2 break;
}
}

Related

Lumen Route redirect to 404 in Nginx

I am doing one project in lumen, and I have installed this in my LAMP server. I have use a htaccess file to strip index.php from url.
here is my htaccess file
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews
</IfModule>
RewriteEngine On
# Redirect Trailing Slashes...
RewriteRule ^(.*)/$ /$1 [L,R=301]
# Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
</IfModule>
Here everything is working fine.
Now I am moving my files to newly created instance with LEMP stack server (nginx). This is lumen file, so I have installed composer in my project directory.
when I am putting a test route in url (browser), (eg: website.com/getUser) it is showing 404 page error.
SO I have modified the nginx default file in /etc/nginx/sites-available/default
server {
listen 80 default_server;
listen [::]:80 default_server ipv6only=on;
root /usr/share/nginx/html;
# Add index.php to the list if you are using PHP
index index.php index.html index.htm index.nginx-debian.html;
server_name _;
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;
include snippets/fastcgi-php.conf;
# fastcgi_pass unix:/run/php/php7.0-fpm.sock;
}
location ~ /\.ht {
deny all;
}
}
and then I have created a separate server block file for my website by copying the default file and making some changes. that file is below.
server {
listen 80 ;
listen [::]:80 ;
root /var/www/html/my.website.com;
server_name my.website.com;
location / {
rewrite ^/(.*)/$ /$1 redirect;
if (!-e $request_filename){
rewrite ^(.*)$ /index.php break;
}
try_files $uri $uri/ =404;
}
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/run/php/php7.0-fpm.sock;
}
location ~ /\.ht {
deny all;
}
}
Now, when I typing my url (website.com/getUser), then it is downloading something. after opening in sublime, I get to know that this file is actually index.php which is in root directory of my project (website.com/index.php)
I am not getting, why it is happening. Why I am not be able to access my route. Where is the problem, can you guys help me in this.
Thanks in advance.
Add this to your config:
location / {
try_files $uri $uri/ /index.php?$query_string;
}

NGINX is serving me php files instead of executing them

I've found many people who had the same problem as me, however I couldn't find the right solution.
I'm running a NGINX server via vagrant and homestead. On the production end I'm using apache and therefore I have an htaccess:
RewriteEngine On
RewriteCond %{REQUEST_URI} !(^cms-system/public|^assets)
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteRule ^(.*)$ cms-system/public/$1 [L]
I use this htaccess to rewrite all urls (except cms-system/public and assets) to my index.php which is in cms-system/public.
I've tried to convert this htaccess to a nginx config with this tool: https://winginx.com/en/htaccess Which did not work very well.. I've made some adjustments. The exception rules work, however I can't get the rewrite for the index.php to work. Could someone please help me?
server {
listen 80;
listen 443 ssl http2;
server_name company.dev;
root "/home/vagrant/company/";
index index.html index.htm index.php;
charset utf-8;
location ~ ^/cms-system/public/(.*) {
}
location ~ ^/assets/(.*) {
}
location / {
if ($request_uri !~ "-f"){
rewrite ^(.*)$ /cms-system/public/$1 break;
}
}
# original location rule
# location / {
# try_files $uri $uri/ /index.php?$query_string;
# }
location = /favicon.ico { access_log off; log_not_found off; }
location = /robots.txt { access_log off; log_not_found off; }
access_log off;
error_log /var/log/nginx/company.dev-error.log error;
sendfile off;
client_max_body_size 100m;
location ~ \.php$ {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_intercept_errors off;
fastcgi_buffer_size 16k;
fastcgi_buffers 4 16k;
fastcgi_connect_timeout 300;
fastcgi_send_timeout 300;
fastcgi_read_timeout 300;
}
location ~ /\.ht {
deny all;
}
ssl_certificate /etc/nginx/ssl/company.dev.crt;
ssl_certificate_key /etc/nginx/ssl/company.dev.key;
}
This is all you need:
location / {
try_files $uri #rewrite;
}
location #rewrite {
if ($uri !~* "^/(cms-system\/public|assets)$") {set $block "A";}
if (!-e $request_filename) {set $block "${block}B";}
if ($block = "AB") {rewrite ^(.*)$ /cms-system/public/$1 last;}
}
instead of:
RewriteEngine On
RewriteCond %{REQUEST_URI} !(^cms-system/public|^assets)
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteRule ^(.*)$ cms-system/public/$1 [L]
This is recommended for security reasons:
location ~* \.(engine|inc|info|install|make|module|profile|test|po|sh|.*sql|theme|tpl(\.php)?|xtmpl)$|^(\..*|Entries.*|Repository|Root|Tag|Template)$|\.php_ {
deny all;
}
Read also:
porting-standard-apaches-mod_rewrite-rules-to-nginx
How do I convert mod_rewrite (QSA option) to Nginx equivalent?

Configring Nginx for rest api

I have implemented Rest Api(Post) in apache server but when i moved it to nginx it not working. Here is configigration file i used in root directory called .htaccess.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-s
RewriteRule ^(.*)$ api.php?rquest=$1 [QSA,NC,L]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^(.*)$ api.php [QSA,NC,L]
RewriteCond %{REQUEST_FILENAME} -s
RewriteRule ^(.*)$ api.php [QSA,NC,L]
</IfModule>
All it does is if a request comes to ipaddess of my server like example.com/promocode_api/validate it sends request to api.php to process it.
But cant able to get configration needed in nginx to make it to work.
Here is my nginx code from etc/nginx/sites-enabled/defalut.
server {
listen 80;
root /purple_dev;
index index.php index.html index.htm;
server_name example.com;
# location / {
# try_files $uri $uri/ /index.html;
# }
location /promocode_api {
}
error_page 404 /404.html;
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /purple_dev;
}
# pass the PHP scripts to FastCGI server listening on the php-fpm socket
location ~ \.php$ {
try_files $uri =404;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
You'll probably need somthing along the lines of:
location /promocode_api {
index api.php;
include mime.types;
try_files $uri $uri/ api.php$is_args$args;
}
I fail to get the point why you have 3 different rules for the same target file though. I would deal with these in php.
Finally i solved it.
This was neede to be added
location /promocode_api {
rewrite ^/promocode_api/(.*)$ /promocode_api/api.php?rquest=$1;
}

Laravel 5.1 giving 404 Not Found except Homepage

I have a website set up on Laravel 5.1. It throws a 404 error on any page except the homepage. However, when I add index.php in the URL, it works. My site runs on a Ubuntu machine with Nginx as the web server.
Loads fine: mysite.com/index.php/dashboard
Gives 404: mysite.com/dashboard
My .htaccess in the public folder:
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews
</IfModule>
RewriteEngine On
RewriteBase /
# Redirect Trailing Slashes If Not A Folder...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ /$1 [L,R=301]
# Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
</IfModule>
Thanks for your time and I would greatly appreciate any help.
Edit:
This is my Nginx conf:
server {
server_name mysite.com;
return 301 $scheme://www.mysite.com$request_uri;
}
server {
listen 80;
server_name www.mysite.com;
# note that these lines are originally from the "location /" block
root /usr/share/web/site/public;
index index.php index.html index.htm;
location / {
try_files $uri $uri/ /index.php?$query_string;
}
error_page 404 /404.html;
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/web/html;
}
location ~ \.php$ {
try_files $uri /index.php =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
# fastcgi_pass 127.0.0.1:9000;
fastcgi_pass unix:/var/run/php-fpm/php-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_max_temp_file_size 0;
fastcgi_buffer_size 4K;
fastcgi_buffers 64 4k;
include fastcgi_params;
}
}
A .htaccess is for Apache. It will not work on Nginx.
Try this in your nginx site configuration (taken from the Laravel documentation.)
location / {
try_files $uri $uri/ /index.php?$query_string;
}

How use htacces to run index.php in subfolder in nginx?

I'm new in nginx.
I stack on run my website using nginx.
I have try to convert htaccess to use nginx.conf or default.d/*.conf but my site still is not working.
Here are my files:
--- .htaccess ---
|-- public |
| --- index.php
| --- .htaccess
|-- application
first htaccess
RewriteEngine on
RewriteRule ^(.*) public/$1 [L]
second htaccess same folder with index.php
Options -MultiViews
RewriteEngine On
Options -Indexes
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^(.+)$ index.php?url=$1 [QSA,L]
This my nginx.conf after edit:
user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;
events {
worker_connections 1024;
}
http {
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /var/log/nginx/access.log main;
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
types_hash_max_size 2048;
include /etc/nginx/mime.types;
default_type application/octet-stream;
include /etc/nginx/conf.d/*.conf;
server {
listen 80 default_server;
listen [::]:80 default_server;
server_name _;
root /usr/share/nginx/html;
include /etc/nginx/default.d/*.conf;
autoindex off;
location / {
# first htaccess configuration
rewrite .* /public/index.php last;
}
error_page 404 /404.html;
location = /40x.html {
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
}
}
}
And the default.d/*.conf
index index.php index.html index.htm;
# htaccess configuration
autoindex off;
if(!-e $request_filename){
rewrite^(.+)$ /index.php?url=$1 break;
}
location ~ \.php$ {
try_files $uri =404;
fastcgi_intercept_errors on;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_pass php-fpm;
}
Can anyone help me to run this in nginx?
One option is to set .../public as the document root, and rewrite any URI beginning with /public/ to /:
root /usr/share/nginx/html/public;
location / {
try_files $uri #index;
}
location #index {
rewrite ^/(.*)$ /index.php?url=$1 last;
}
location /public/ {
rewrite ^/public(.*)$ $1 last;
}
location ~* \.php$ {
try_files $uri =404;
...
}
However, if you prefer to use your current document root, this may suit your requirements:
root /usr/share/nginx/html;
location / {
try_files $uri /public$uri #index;
}
location #index {
rewrite ^/(.*)$ /index.php?url=$1 last;
}
location ~* \.php$ {
try_files $uri /public$uri =404;
...
}

Categories