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..
}
}
Related
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;
}
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;
...
}
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
Here is the rule in English:
Any HTTP request other than those for index.php, assets folder, files folder and robots.txt is treated as a request for your index.php file.
I have an .htaccess file that works correctly on Apache server:
RewriteCond $1 !^(index\.php|assets|files|robots\.txt)
RewriteRule ^(.*)$ index.php/$1 [L]
Some correct results for this rule:
example.com = example.com/index.php
example.com/index.php/welcome = example.com/welcome
example.com/assets/css/main.css != example.com/index.php/assets/css/main.css
I tried some tools to convert from htaccess rule to nginx rule but all were incorrect.
Via http://winginx.com/htaccess (missing the exception rules for assets folder...):
location / { rewrite ^(.*)$ /index.php/$1 break; }
Via http://www.anilcetin.com/convert-apache-htaccess-to-nginx/ (error in $1 value):
if ($1 !~ "^(index.php|assets|files|robots.txt)"){
set $rule_0 1$rule_0;
}
if ($rule_0 = "1"){
rewrite ^/(.*)$ /index.php/$1 last;
}
How can I fix this? It's really hard to debug the rule.
Here is my nginx config so far:
server {
listen 80;
server_name www.example.com example.com;
location / {
try_files $uri $uri/ /index.php?/$request_uri;
}
location ~ \.php$ {
root html;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /var/www/html$fastcgi_script_name;
include fastcgi_params;
}
location ~ /\.ht {
deny all;
}
}
You can add this to your config:
location ~* ^/(assets|files|robots\.txt) { }
This will work correctly with your location / rule.
Your config also need to add root document and default index file.
...
root /ftp/wardrobe;
index index.php index.html index.htm;
location / {
try_files $uri $uri/ /index.php?/$request_uri;
}
location ~* ^/(assets|files|robots\.txt) { }
...
You want to do this instead:
if ($request_uri !~ ^/(index\.php|assets|files|robots\.txt)) {
rewrite ^/(.*)$ /index.php/$1 last;
}
$request_uri is for the original URI request by the client. If you want the URI request AFTER other Nginx rewrite rules have processed it then you would use $uri. However, for what you are trying to do the prior would be the one you would want.
Also, you need to escape special regular expression characters like . by using a backslash.
Please try this. It works for me.
server {
server_name domain.tld;
root /var/www/codeignitor;
index index.html index.php;
# set expiration of assets to MAX for caching
location ~* \.(ico|css|js|gif|jpe?g|png)(\?[0-9]+)?$ {
expires max;
log_not_found off;
}
location / {
# Check if a file or directory index file exists, else route it to index.php.
try_files $uri $uri/ /index.php;
}
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;
}
}
I almost have it working, but even then not sure if it's properly implemented. (I'm new to Nginx!)
My test has this directory structure:
/index.php (root directory front controller)
/test/index.php (sub directory front controller)
/include/include.php (some PHP include)
Files named index.php are the front controllers.
The Nginx server block:
server {
listen 80;
server_name mysite.com;
root /var/www/mysite.com/http;
index index.php;
location / {
try_files $uri $uri/ /index.php;
}
location ~ \.php$ {
try_files $uri #rewrite;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
location #rewrite {
rewrite ^ /index.php last;
}
}
These cases currently work and need to work like this:
1. http://mysite.com/ => /index.php (exists)
2. http://mysite.com/test => /test/index.php (exists)
3. http://mysite.com/test/index.php => /test/index.php (exists)
4. http://mysite.com/test/foobar => /index.php (/test/foobar didn't exist)
5. http://mysite.com/asdf => /index.php (/asdf didn't exist)
6. http://mysite.com/asdf.php => /index.php (/asdf.php didn't exist)
7. http://mysite.com/asdf/index.php => /index.php (didn't exist; the try_files #rewrite fixes this, otherwise I get "No input file specified.")
The case I don't have yet is:
8. http://mysite.com/include/include.php => /index.php
Currently accessing a PHP file that exists that isn't a front controller is processing that script. I can understand why it's doing it, it's try(ing)_files $uri #rewrite and finding include.php. I've been trying to fix it with rewrites, but no dice. Or is there a better way, with location blocks/try_files?
In general am I going about this server block correctly for my intentions?
How about ...
server {
listen 80;
server_name mysite.com;
root /var/www/mysite.com/http;
index index.php;
location / {
try_files $uri $uri/ /index.php;
}
location ~ \.php$ {
location ~ /index\.php$ {
if (!-e $request_filename) {
rewrite ^ /index.php break;
}
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
rewrite ^ /index.php last;
}
}
This will serve ...
yoursite/any_path/ => yoursite/any_path/index.php
yoursite/any_path/index_file.php => yoursite/any_path/index.php
yoursite/any_path/any_non_index_file.php => yoursite/index.php
... which is what your question suggests you want (last one seems strange to me)