In apache I have an .htaccess that will rewrite from http://server/api/any/path/i/want to http://server/api/index.php if no file or folder is found.
Options -MultiViews
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond $0#%{REQUEST_URI} ([^#]*)#(.*)\1$
RewriteRule ^.*$ %2index.php [L,NC,QSA]
</IfModule>
I'm moving to docker and will use nginx instead and I want to re-write the rewrite.
Important to note is that using apacheand .htaccess $_SERVER['REQUEST_URI'] is /api/any/path/i/want and not the re-written url (index.php....).
I'm not that well versed with nginx but from posts on SO I've figured some things out.
Relevant section of site.conf
location / {
root /app/html;
try_files $uri $uri/ index.html /index.php?$args;
}
location ~ \.php$ {
try_files $uri #missing;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass php:9000;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info;
}
location #missing {
rewrite ^ $scheme://$host/api/index.php permanent;
}
The above config will unfortunately only redirect to index.php and is as far I've managed to get.
How can I do the same thing in nginx?
This is a typical nginx configuration for PHP-FPM.
server {
root /app/html;
location / {
try_files $uri $uri/ /api/index.php$is_args$args;
}
location ~ \.php$ {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass php:9000;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info;
}
}
Notice the differences from your example:
Removed the unnecessary #missing location block.
Removed the try_files statement from the .php location block.
Moved the root declaration to the server block. If you need to have different roots, please specify this in your question.
The only try_files statement includes the full path for your api/index.php.
If a request comes for a non-existing path, it will be handled by your /app/html/api/index.php script, acting as a global entry-point.
Related
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;
...
}
Why displaying error ?
I have running other laravel projects fine with laravel 5.2 but i have old project with laravel 4.2 where i am getting this error :
404 File Not Found
You can check error here
On my local pc with php7 fpm working fine using Laravel Valet.
Here is my nginx config file :
server {
listen 80;
server_name blog.yourserviceconnection.com;
root var/www/html/blog/public/;
index index.php index.html index.htm;
location / {
try_files $uri $uri/ /index.php;
}
location ~ \.(hh|php)$ {
fastcgi_keep_conn on;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
Looks everything fine to me, and this is .htaccess file :
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews
</IfModule>
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
</IfModule>
I saw you are using hhvm so Instead adding.
location ~ \.(hh|php)$ {
fastcgi_keep_conn on;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
use
include hhvm.conf
May this will help as it's default configuration by hhvm so no mistake.
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..
}
}
I have a bunch of different rules on my nginx server with php-fpm.
The simple one is a rewrite that changes http://server/$1?param=1 to http://server/$1.php?param=1 using
location #extensionless-php {
rewrite ^(.*)$ $1.php last;
}
I also need to rewrite http://server/abc/123 to http://server/abc.php/123 and also have this processed by the php-fpm
This is the fast-cgi code:
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;
}
and this is the location rewrite
location #abc-php {
rewrite ^(.*)/abc/(.*)$ $1/photo.php/$2 last;
}
But I keep getting a 404. I'm not sure where I'm going wrong and any help would be appreciated.
You don't show how the named location #abc-php is invoked. I suspect that you have something like this to managed the extensionless PHP:
location / {
try_files $uri $uri/ #extensionless-php;
}
Your new rewrite rule can be added to the existing named location, like this:
location #extensionless-php {
rewrite ^(.*)/abc/(.*)$ $1/photo.php/$2 last;
rewrite ^(.*)$ $1.php last;
}
However, your fast-cgi block is incapable of handing the path_info, so you will need to look here for conventional wisdom, or use something like this:
location ~ ^(?<script>.*\.php)(?<pathinfo>.*)$ {
try_files $script =404;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PATH_INFO $pathinfo;
fastcgi_pass unix:/var/run/php5-fpm.sock;
}
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;
}
}