Nginx config for pretty URLs - php

Previously I was working with Apache server (I had a special .htaccess) for my router, and everything worked as expected:
request: test.local/index/action was handled by test.local/index.php.
My .htaccess file was:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php
The problem: Now, I need to implement same functionality in Nginx. I implement nginx configs for my websites including in main nginx.conf -nginx folder- /include.d/test.conf . I tried this test.conf configuration:
server {
listen 80;
listen [::]:80;
server_name productlocator.local;
root /var/www/test/;
index index.php index.html;
location ~ \.php$ {
# fastcgi_split_path_info ^(.+?\.php)(/.*)$;
try_files $uri $uri/ /index.php?$query_string;
if (!-f $document_root$fastcgi_script_name) {
return 404;
}
fastcgi_pass 127.0.0.1:9000;
fastcgi_param SCRIPT_FILENAME $request_filename;
include fastcgi_params;
}
}
Requests to test.local/index.php are properly handled, but requests like test.local/index/action result with 404 Not Found error.
The question: How do I configure Nginx for my router to work?

Use
server_name test.local;
instead
server_name productlocator.local;
And then look this link

Related

Nginx: config set up to make extensionless php pages work

I am trying to set up my first website with nginx and I cannot seem to make extensionless php pages to work.
I would like my pages like /aboutme.php to appear as /aboutme or I would like to just change my URLs to /aboutme instead of /aboutme.php and make those work.
I managed to make the last scenario work on cpanel hosting by editing .htaccess like this:
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule !.*\.php$ %{REQUEST_FILENAME}.php [L, QSA]
However, if I convert htaccess code to nginx using online tools, it doesn't work. I also tried to edit my nginx.conf with try_files and extensionless-php with no success, I get 404. My plain nginx.conf without try_files and extensionless-php looks like this:
server {
listen localhost:8081;
server_name mywebsite.com; # I use my website here
root /usr/share/nginx/web/website1;
location / {
root /usr/share/nginx/web/website1;
index index.php index.html index.htm;
}
location ~ \.php$ {
root /usr/share/nginx/web/website1;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
Edit: my question got marked as a duplicate elsewhere, so I tried to configure it as it was said with no success, if I visit '/aboutme' I get '404 not found', if I visit '/aboutme.php', it's opened as '/aboutme.php' with '.php':
server {
listen localhost:8081;
server_name mywebsite.com;
root /usr/share/nginx/web/website1;
location / {
try_files $uri $uri/ #extensionless-php;
index index.html index.htm index.php;
}
location ~ \.php$ {
try_files $uri =404;
include /etc/nginx/fastcgi_params;
fastcgi_pass 127.0.0.1:9000;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
location #extensionless-php {
rewrite ^(.*)$ $1.php last;
}
}
error log: 2018/03/14 14:39:32 [error] 7714#7714: *27 open() "/usr/share/nginx/web/website1/aboutme" failed (2: No such file or directory), client: 127.0.0.1, server: mywebsite.com, request: "GET /about HTTP/1.1", host: "mywebsite.com", referrer: "mywebsite.com/"; there is aboutme.php present in directory, nginx version 1.10.3
I tried restarting nginx and vps multiple times.

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

Existing Apache .htaccess to Nginx Config

I think ive got lost a little bit.
I tried a few solutions, but i dont get one part. Everything has to be handled by index.php and i can not get it work in nginx. I keep learning Nginx as its awesome but would really appreciate some quick help from somebody.
My .htaccess:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?_route_=$1 [L,QSA]
And my Nginx config is :
server {
listen 80;
listen [::]:80;
root /folder;
index index.php index.html index.htm index.nginx-debian.html;
server_name hostname.com;
location / {
try_files $uri $uri/ =404;
}
location ~ \.php$ {
try_files = $uri #missing;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/run/php/php7.0-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
location #missing {
rewrite ^ $scheme://$host/index.php permanent;
}
}
Can somebody point me to the right direction? Thanks
Any URI which does not end with .php is processed by the location / block. If you want to implement your Apache .htaccess rules, that is where it should be done. See this document for more.
For example:
location / {
try_files $uri $uri/ /index.php?_route_=$uri;
}
In the location ~ \.php$ block, you should guard against passing uncontrolled requests to PHP, for example:
location ~ \.php$ {
try_files $uri =404;
...
}

Permalinks in Wordpress not working with Laravel + Nginx

I have a site developed with Laravel on my main route say www.example.com/. I have configured it properly with Nginx and php-fpm. My config is below.
Then I added a blog in route /blog (www.example.com/blog/) and configured it with Nginx alias.
Now the problem is that Permalinks in Wordpress are not working. Nginx redirects to Laravel's 404 page.
For example when user enters some URL like this: example.com/blog/about, Laravel's 404 page shows up which is weird.
How can I fix this? How can I config Nginx? What's Wrong?
server {
listen 80;
server_name example.com;
root /usr/share/nginx/html/;
location /blog {
try_files $uri $uri/ /index.php?$args;
alias /usr/share/nginx/blog/;
index index.php index.html index.htm;
location ~ \.php$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME /usr/share/nginx$fastcgi_script_name;
}
}
location / {
root /usr/share/nginx/main_site;
index index.php index.html index.htm;
try_files $uri $uri/ /index.php$is_args$args;
location ~ \.php$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
}
You do not need to use alias when the location matches the end of the alias path. See this document.
The try_files in location /blog needs to default to the WordPress router (/blog/index.php) and not the Laravel router (/index.php).
Try:
location /blog {
try_files $uri $uri/ /blog/index.php?$args;
root /usr/share/nginx;
...
location ~ \.php$ {
...
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
}
Suppose you are running laravel on your server and you want to use wordpress as a blog. and you installed wordpress in blogs named folder. Then you have to do changes in 3 places
1) laravel .htaccess file
2) blogs folder .htaccess file
3) nginx configuration file
Laravel.htaccess file changes add this line
RewriteCond %{REQUEST_URI} !^/blogs/
blogs folder .htaccess file add this code or modify
RewriteEngine On
RewriteBase /blogs/ # <==== CHANGED LINE!!
RewriteRule ^index.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /blogs/index.php [L] # <==== CHANGED LINE!!!
in nginx configuration file try modify below code in both port 80 and port 443 code block
location /blogs {
try_files $uri $uri/ /blogs/index.php?$args;
set $base /var/www/html;
root $base/public;
location ~ \.php$ {
fastcgi..
}
}

Rewrite all requests for non existing files to index.php with try_files with Nginx

I am trying to convert trivial htaccess file to Nginx and can't make it work. It returns 404 error.
Here is htaccess content:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php [L]
Here is my current nginx config:
server {
listen 80;
server_name domain.biz;
root /var/www/domain.biz;
charset utf-8;
autoindex off;
location / {
try_files $uri $uri/ /index.php?$args;
}
location ~ \.php$ {
include fastcgi_params;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_param SCRIPT_FILENAME /var/www/domain.biz$fastcgi_script_name;
}
}
How about other php files you call directly? For example an info.php with just a
phpinfo();
inside?
I ask this because your server conf seems to be using try_files just right, but I'm not sure you're serving php scripts right.
¿Is your fastcgi pool listening on that sock? ¿Are you sure it isn't listening in port 9000 for example? In any case, I prefer to define an upstream in the http section and use it later in the server section
http {
upstream phpbackend {
server unix:/var/run/php5-fpm.sock;
}
...
server {
...
location ~ \.php$ {
include fastcgi_params;
fastcgi_pass phpbackend;
fastcgi_param SCRIPT_FILENAME /var/www/domain.biz$fastcgi_script_name;
}
}
}
Are you sure your php.ini has the cgi.fix_pathinfo set to false?

Categories