NGINX is serving me php files instead of executing them - php

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?

Related

Can't convert .htaccess to nginx directives

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

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

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

Running a secondary PHP app from a subdirectory in Nginx

I have a site built on the Kirby flat file CMS and its running in the root directory just fine, but I'm trying to get an instance of FlightPHP running in a subdirectory (called crm) of the above project. This FlightPHP instance will handle form submissions so I need to be able to map the urls correctly.
With Apache and .htaccess, it's easy:
# make forms/crm links work
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^crm/(.*) crm/index.php [L]
I just can't seem to get this working with Nginx, here's what I have so far:
server {
listen 80;
listen [::]:80;
root /var/www/mainsite;
index index.php index.html index.htm;
server_name mydomain.com;
autoindex on;
access_log off;
# Kirby Specific Directories
rewrite ^/site/(.*) /error permanent;
rewrite ^/kirby/(.*) /error permanent;
add_header X-UA-Compatible "IE=Edge,chrome=1";
location ~ .php {
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_hide_header X-Powered-By;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_script_name;
fastcgi_buffer_size 128k;
fastcgi_buffers 256 16k;
fastcgi_busy_buffers_size 256k;
fastcgi_temp_file_write_size 256k;
include fastcgi_params;
}
location ^~ /crm {
root /var/www/mainsite/crm;
if (!-e $request_filename)
{
rewrite ^/(.*)$ /index.php/$1 last;
break;
}
}
location / {
try_files $uri $uri/ /index.php?$query_string;
}
location ~* .(?:xml|ogg|mp3|mp4|ogv|svg|svgz|eot|otf|woff|ttf|css|js|jpg|jpeg|gif|png|ico)$ {
try_files $uri =404;
expires max;
access_log off;
log_not_found off;
add_header Pragma public;
add_header Cache-Control "public, must-revalidate, proxy-revalidate";
}
}
I have tried quite a few approaches from similar posts online, but none that work correctly.
Try replacing your location ^~ /crm block with this
location /crm {
try_files $uri $uri/ /crm/index.php?$args;
}
And if that one doesn't solve it try
location /crm {
try_files $uri $uri/ /crm/index.php?$uri;
}

How to use Nginx to separate variables with slashes

I'd like to be able to use
www.example.com/profiles/1234567890
instead of
www.example.com/profiles?id=1234567890
but was unable to figure out how to do so.
I found an apache equivalent here: Turn text after slashes into variables with HTACCESS but I don't know how to get it to work in Nginx. I only need the one variable which is id.
Additionally, is it possible to do a rewrite such that
www.example.com/id/1234567890
points to
www.example.com/profiles/1234567890
but the url doesn't change?
Nginx config
server {
server_name www.domain.com;
rewrite ^(.*) http://domain.com$1 permanent;
}
server {
listen 80;
server_name domain.com;
root /var/www/domain.com/public;
index index.php;
access_log /var/www/domain.com/access.log;
error_log /var/www/domain.com/error.log;
rewrite ^/profiles?id=(.*)$ /profiles/$1 last;
rewrite ^/id/(.*)$ /profiles/$1 last;
# unless the request is for a valid file, send to bootstrap
if (!-e $request_filename)
{
#rewrite ^(.+)$ /index.php?q=domain.com last;
rewrite ^(.*)$ $1.php last;
}
# catch all
error_page 404 /index.php;
# Directives to send expires headers and turn off 404 error logging.
location ~* \.(js|css|png|jpg|jpeg|gif|ico)$ {
expires max;
log_not_found off;
access_log off;
}
location /phpmyadmin {
root /usr/share/;
index index.php index.html index.htm;
location ~ ^/phpmyadmin/(.+\.php)$ {
try_files $uri =404;
root /usr/share/;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include /etc/nginx/fastcgi_params;
}
location ~* ^/phpmyadmin/(.+\.(jpg|jpeg|gif|css|png|js|ico|html|xml|txt))$ {
root /usr/share/;
}
}
location /phpMyAdmin {
rewrite ^/* /phpmyadmin last;
}
location = /favicon.ico {
log_not_found off;
access_log off;
}
location = /robots.txt {
allow all;
log_not_found off;
access_log off;
}
## Disable viewing .htaccess & .htpassword
location ~ /\.ht {
deny all;
}
location / {
# This is cool because no php is touched for static content.
# include the "?$args" part so non-default permalinks doesn't break when using query string
try_files $uri $uri/ #extensionless-php?$args;
}
location #extensionless-php {
rewrite ^(.*)$ $1.php last;
}
# use fastcgi for all php files
location ~ \.php$
{
try_files $uri =404;
fastcgi_pass 127.0.0.1:9000;
#fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include /etc/nginx/fastcgi_params;
# Some default config
fastcgi_connect_timeout 20;
fastcgi_send_timeout 180;
fastcgi_read_timeout 180;
fastcgi_buffer_size 128k;
fastcgi_buffers 4 256k;
fastcgi_busy_buffers_size 256k;
fastcgi_temp_file_write_size 256k;
fastcgi_intercept_errors on;
fastcgi_ignore_client_abort off;
}
}
Inside the server block of your configuration file, try adding this:
rewrite ^/profiles?id=(.*)$ /profiles/$1 last;
rewrite ^/id/(.*)$ /profiles/$1 last;
Restart nginx. It should work.
More information can be found here: http://nginx.org/en/docs/http/ngx_http_rewrite_module.html
I think you put the rules swapped, try something like
rewrite ^/(?:profiles|id)/(.*) /profiles?id=$1 last;
For anyone looking for the answer, this worked:
rewrite ^/profiles/([a-zA-Z0-9_-]+)$ /profile.php?id=$1;
rewrite ^/id/([a-zA-Z0-9_-]+)$ /profile.php?id=$1;

Categories