I am migrating some vhosts from apache to nginx. Unfortunately i am having some troubles because of .htaccess files from the apache instance.
Here is the situation (apache):
The DocumentRoot has different folders with projects inside. I could access most of them (through www.example.com/appname) without problems.
But on accessing (the main page) www.example.com the following .htaccess file redirects the users.
/var/www/html/.htaccess:
Redirect /index.html https://example.com/example/
Which then has another .htaccess in /var/www/html/example/.htaccess
Options -Indexes
IndexIgnore */*
Options FollowSymlinks
RewriteEngine on
RewriteRule ^(.+)?$ web/$1
So then the next .htaccess in /var/www/html/example/web
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php
How can i translate this to an nginx location block or blocks ? I can't seem to get it right. On my last check the client browser said that something is wrong with redirecting. Here is the nginx vhost config:
Please keep in mind that there are still some tries i found, but so far i didn't have any luck. Help would be really appreciated !
thank you guys
server {
server_name example.com www.example.com;
listen 80;
root /var/www/html;
index index.php index.html index.htm;
access_log /dev/stdout;
error_log /dev/stdout info;
# FIRST .htaccess FILE
# redirect index.html to example.com/example
# the first line seems to have worked, the 2nd one is not yet tested
location /index.html { rewrite ^(.*)$ http://example.com/example/ redirect; }
location /index.html { return $301 $scheme:http://example.com/example/$request_uri; }
location / {
try_files $uri $uri/ /index.php?$args /index.html;
#try_files $uri /index.php?$args /index.html;
}
location /example {
root /var/www/html;
# try_files options:
# try_files $uri $uri/ /index.php?$args;
# try_files $uri /index.php?url=$request_uri;
# SECOND .htaccess FILE
rewrite ^/(.+)?$ /web/$1;
location /example/web {
# THIRD .htaccess FILE
# option 1
# if (!-e $request_filename){ rewrite ^(.*)$ /index.php; }
# option 2
#rewrite ^/(.*)$ /$1.php last;
try_files $uri $uri/ /index.php$args;
}
}
location ~ \.php$ {
include fastcgi_params;
fastcgi_pass php5:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
# deny access to .htaccess files, if Apache's document root
# ocurs with nginx's one
#
location ~ /\.ht {
deny all;
}
}
check this out: Apache mod_rewrite to Nginx rewrite rules.
basically, apache rewrite and nginx rewrite do not quite mean the same thing. it seems that you are using nginx rewrite where you should be using nginx try_files.
so, you might try doing
try_files /web/$uri /web/$uri/ /web/index.php?$args
instead of
rewrite ^/(.+)?$ /web/$1;
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;
}
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
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;
}
I'm using Nginx and Codeigniter alongside php5-fpm. Everything "seems" to work just fine, pages are shown and everything looks great. Ofcource this isn't the reason I'm asking this question here, I actually have a problem.
The problem I'm facing is that 404 error is thrown with page, even though the page is rendered properly, I'm stilling getting 404's(in the logs).
The reason why I'm getting 404's (after looking at Nginx's error logs), is that Nginx can't open the file I'm requesting, because Nginx tries to open the PHP files directly, instead of referring to the controller/method and unfortunately Codeigniter works that way.
For example:
requesting http://website.com/<controller>/<function>/<other_params> results in 404 in Nginx logs, the reason behind this is that open() can't open the specified directory because it doesn't exists, instead of referring to the controller/method.
Some important logs:
Nginx error log:
[error] 4172#0: *482 open() "/var/www/<domain>/public/site/section/main" failed
(2: No such file or directory), client: <client_ip>, server: <domain>, request: "GET
/site/section/main HTTP/1.1", host: "<domain>"
as I said before, Nginx is trying to access the file directly, instead of making Codeigniter deal with it.
my sites-enabled/ci configurations:
server
{
server_name <domain> *.<domain>;
access_log /var/www/<domain>/access.log;
error_log /var/www/<domain>/error.log;
root /var/www/<domain>/public;
index index.php index.html index.htm;
location ~* \.(jpg|jpeg|gif|png|css|js|ico|xml)$ {
access_log off;
log_not_found off;
expires 360d;
}
# enforce www (exclude certain subdomains)
# if ($host !~* ^(www|subdomain))
# {
# rewrite ^/(.*)$ $scheme://www.$host/$1 permanent;
# }
# enforce NO www
if ($host ~* ^www\.(.*))
{
set $host_without_www $1;
rewrite ^/(.*)$ $scheme://$host_without_www/$1 permanent;
}
# canonicalize codeigniter url end points
# if your default controller is something other than "welcome" you should change the following
if ($request_uri ~* ^(/site(/index)?|/index(.php)?)/?$)
{
rewrite ^(.*)$ / permanent;
}
# removes trailing "index" from all controllers
if ($request_uri ~* index/?$)
{
rewrite ^/(.*)/index/?$ /$1 permanent;
}
# removes trailing slashes (prevents SEO duplicate content issues)
# if (!-d $request_filename)
# {
# rewrite ^/(.+)/$ /$1 permanent;
# }
# removes access to "system" folder, also allows a "System.php" controller
if ($request_uri ~* ^/(system|application))
{
rewrite ^/(.*)$ /index.php?/$1 last;
break;
}
# unless the request is for a valid file (image, js, css, etc.), send to bootstrap
#if (!-e $request_filename)
#{
# rewrite ^/(.*)$ /index.php?/$1 last;
# break;
#}
# catch all
error_page 404 /index.php;
# use fastcgi for all php files
location ~ \.php($|/)
{
#if (!-e $request_filename) {
# return 404;
# }
fastcgi_pass 127.0.0.1:9000;
#fastcgi_pass php5-fpm-sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
# location / {
# try_files $uri $uri/ #codeigniter;
#}
# location #codeigniter {
# rewrite ^(.*) /index.php?$1 last;
# }
# deny access to apache .htaccess files
location ~ /\.ht
{
deny all;
}
}
So, what's the reason behind this misunderstanding between Nginx and Codeigniter?
Thanks in advance.
It seems like there's no rule in your config that tells nginx to try sending requests to php when the file is not found. There's a commented out block with try_files and #codeigniter that almost look like it. In theory this is what you want nginx do:
Check if the url exists, if does serve it.
Send everything else to codeigniter and let it sort out.
For this, these two blocks should be enough:
location / {
# Check if a file exists, or route it to index.php.
try_files $uri $uri/ /index.php;
}
location ~ \.php$ {
# for security, see http://forum.nginx.org/read.php?2,88845,page=3
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(.*)$;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
The other if guarded block shouldn't break with these in place.
I'm attempting to set up the Laravel PHP Framework to work with Nginx. Here is my directory structure:
/project
/application
/laravel
/public
index.php
/legacy
/index.php
/stylesheets
default.css
Basically what I have is a standard Laravel download w/ a legacy folder thrown in which holds all of the files from my non-MVC project.
I need Nginx to first check if the requested page/file exists inside of legacy, if it does then I want to use that. Otherwise, I want to fall back to Laravel's index.php file which is located in project/public/.
I'm no expert when it comes to Nginx configurations so any help that you can provide would be most appreciated.
server {
server_name .laravel.dev;
root /home/tamer/code/laravel/public;
index index.php index.html;
#browse folders if no index file
autoindex on;
# serve static files directly
location ~* \.(jpg|jpeg|gif|css|png|js|ico|html)$ {
access_log off;
expires max;
}
# removes trailing slashes (prevents SEO duplicate content issues)
if (!-d $request_filename)
{
rewrite ^/(.+)/$ /$1 permanent;
}
# enforce NO www
if ($host ~* ^www\.(.*))
{
set $host_without_www $1;
rewrite ^/(.*)$ $scheme://$host_without_www/$1 permanent;
}
# canonicalize codeigniter url end points
# if your default controller is something other than "welcome" you should change the following
if ($request_uri ~* ^(/lobby(/index)?|/index(.php)?)/?$)
{
rewrite ^(.*)$ / permanent;
}
# removes trailing "index" from all controllers
if ($request_uri ~* index/?$)
{
rewrite ^/(.*)/index/?$ /$1 permanent;
}
# unless the request is for a valid file (image, js, css, etc.), send to bootstrap
if (!-e $request_filename)
{
rewrite ^/(.*)$ /index.php?/$1 last;
break;
}
# catch all
error_page 404 /index.php;
location ~ \.php$ {
try_files $uri =404;
fastcgi_pass unix:/tmp/php.socket;
fastcgi_index index.php;
#include fastcgi_params;
include /home/tamer/code/nginx/fastcgi_params;
}
access_log /home/tamer/code/laravel/storage/logs.access.log;
error_log /home/tamer/code/laravel/storage/logs.error.log;
}