I use Nginx with my webserver, and don't fully understand how the rewrite works here. I got it working on Apache.
I use MVC based PHP project, and i want to use clean URL's as i have done with Apache.
www.domain.com/index.php?url=home/index works fine.
www.domain.com/home/index does not work.
As you can see, i want to have clean URL's like the last one.
I've tried several rewrites, and don't know if I should use try_files or rewrite. But I guess it's rewrite im after.
As I said, I'm still learning the rewrite.
My server block looks like this:
server {
listen 80 default_server;
listen [::]:80 default_server ipv6only=on;
root /usr/share/nginx/html/pub/;
index index.php index.html index.htm;
server_name localhost;
location / {
try_files $uri $uri/ #rewrite;
}
location #rewrite {
rewrite ^/index.php?$arg_url /$arg_url permanent;
}
location ~ \.php$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
include fastcgi_params;
}
}
I thought my rewrite block made sense, but I guess it doesn't. Tried several rewrites, either I get 500 or 404 error. And as I said, I don't get rewrites yet. Need some help to get started.
Thanks in advance.
Not an expert with nginx either but I think you are looking for something like this:
location / {
index index.html index.htm index.php;
if (!-d $request_filename) {
rewrite ^/(.*)$ /index.php?url=$1 last;
break;
}
}
Then you can remove that rewrite directive
The rest of my config looks like this:
location ~ \.php$ {
root /var/www/path;
fastcgi_split_path_info ^(.+\.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;
fastcgi_buffer_size 128k;
fastcgi_buffers 2564k;
fastcgi_busy_buffers_size 256k;
fastcgi_temp_file_write_size 256k;
}
location ~ /\.git {
denyall;
}
#Staticfileslocation
location ~* ^.+.(swf|jpg|jpeg|gif|png|ico|css|bmp|js|zip)$ {
expires max;
root /var/www/path;
}
Try with this:
server {
listen 80 default_server;
listen [::]:80 default_server ipv6only=on;
root /usr/share/nginx/html/pub/;
index index.php index.html index.htm;
server_name localhost;
location / {
try_files $uri $uri/ #rewrite;
}
location #rewrite {
rewrite ^/(.*)$ /index.php?url=$1 last;
}
location ~ \.php$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
include fastcgi_params;
}
}
Related
I have a website
https://dev.mywebsite.com/index.php?album=portraits
which displays photo-albums dynamically based on a POST value.
I want to rewrite this URL to this:
https://dev.mywebsite.com/portraits
But my Nginx rewrite rule is not working. Nothing is happening when I enter https://dev.mywebsite.com/index.php?album=portraits. And no page is found when entering https://dev.mywebsite.com/portraits.
I don't know what I am doing wrong.
This is the code I'm trying to use currently:
location / {
rewrite ^/(.*)$ /album.php?album=$1 last;
}
I've also tried this:
location = /portraits {
rewrite ^/portraits?$ /index.php?album=portraits break;
}
and this:
location = /album {
rewrite ^album/([a-z]+)/?$ album.php?album=$1 break;
}
This is the entire nginx site-config file i'm using:
server {
listen 80 default_server;
listen 443 ssl;
root /config/www;
index index.php index.htm index.html;
server_name dev.mywebsite.com;
ssl_certificate /config/keys/cert.crt;
ssl_certificate_key /config/keys/cert.key;
client_max_body_size 0;
location / {
try_files $uri $uri/ /index.html /index.php?$args =404;
}
location / {
rewrite ^/(.*)$ /album.php?album=$1 last;
}
location ~ \.php$ {
#fastcgi_split_path_info ^(.+\.php)(/.+)$;
# With php5-cgi alone:
fastcgi_pass 127.0.0.1:9000;
# With php5-fpm:
#fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
include /etc/nginx/fastcgi_params;
}
}
If the file you really have is index.php, then you can rewrite (without any additional location) with:
rewrite ^/portraits$ /index.php?album=portraits last;
Keep the location ~ \.php$ { of course :)
I move my Friendly URL's from Apache to nginx and I have a problem. I want to Friendly URL's only works within the subdirectory sgforum.
In PHP, I receive the addresses as: 127.0.0.1/sgforum/index, 127.0.0.1/sgforum/member etc.
When I go on 127.0.0.1/sgforum/ - it works, but when I give member (127.0.0.1/sgforum/member), or index, it downloads a file to my computer, instead of opening with php.
This is my /etc/nginx/sites-available/default file:
server {
listen 80 default_server;
#listen [::]:80 default_server;
root /home/ariel/workspace;
index index.php index.html;
server_name _;
location / {
try_files $uri $uri/ =404;
}
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/run/php/php7.0-fpm.sock;
}
# FRIENDLY URLS
location /sgforum/ {
if (!-e $request_filename){
rewrite ^/sgforum/(.*)$ /sgforum/index.php break;
}
}
location ~ /\.ht {
deny all;
}
}
I changed it, and finally works as it should.
# FRIENDLY URLS
location /sgforum/ {
try_files $uri $uri/ /sgforum/index.php;
}
you have to set location for member folder
try change
location ~ \.php$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
I want to be able to convert the following link:
http://domain.com/foo/bar
into http://domain.com/index.php?controller=foo&action=bar
using php5-fpm. I also want to be able to access static files inside www/ folder. How do I do that? This is what I have so far:
server {
charset utf-8;
client_max_body_size 8M;
listen 80; ## listen for ipv4
server_name domain.com;
root /var/www/domain.com/www;
index index.php;
access_log /var/log/nginx/domain.com.access.log;
error_log /var/log/nginx/domain.com.error.log;
location / {
rewrite ^([a-zA-Z0-9]+)/([a-zA-Z0-9]+)/?$ /index.php?controller=$1&action=$2;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
include fastcgi_params;
}
location ~ /\.(ht|svn|git) {
deny all;
}
}
But it gives me blank page and no get parameters. How should I do this?
The try_files directive is useful when serving static files, if they exist, and rewriting the URI if they do not. See this document for details.
There are a number of ways to achieve this, for example:
location / {
try_files $uri $uri/ #rewrite;
}
location #rewrite {
rewrite ^([a-zA-Z0-9]+)/([a-zA-Z0-9]+)/?$ /index.php?controller=$1&action=$2 last;
return 404;
}
location ~ \.php$ {
try_files $uri =404;
fastcgi_pass unix:/var/run/php5-fpm.sock;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $request_filename;
}
I'm trying to configure nginx to serve 2 different php scripts from 2 different location. The configuration is as follows.
I have a Laravel installation which resides in /home/hamed/laravel in which its public directory should be served.
I have a Wordpress installation in /home/hamed/www/blog.
And this is my nginx configuration:
server {
listen 443 ssl;
server_name example.com www.example.com;
#root /home/hamed/laravel/public;
index index.html index.htm index.php;
ssl_certificate /root/hamed/ssl.crt;
ssl_certificate_key /root/hamed/ssl.key;
location /blog {
root /home/hamed/www/blog;
try_files $uri $uri/ /blog/index.php?do=$request_uri;
}
location / {
root /home/hamed/laravel/public;
try_files $uri $uri/ /index.php?$request_uri;
}
error_page 404 /404.html;
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
location ~ \.php$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
#fastcgi_pass 127.0.0.1:9000;
fastcgi_pass unix:/var/run/php5-fpm.hamed.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
The problem is when trying to access the wordpress section by calling example.com/blog still the the laravel installtion takes over the request.
Now I have tried replacing root directive inside location blocks with alias to no avail.
According to this guide having the index directive or try_files inside location triggers an internal redirect which I suspect causes this behavior.
Would someone please help me figure this out?
The problem is that location ~ \.php$ { ... } is responsible for handling all of your php scripts, which are divided across two different roots.
One approach is to use a common root for the server container and perform internal rewrites within each prefix location block. Something like:
location /blog {
rewrite ^(.*\.php)$ /www$1 last;
...
}
location / {
rewrite ^(.*\.php)$ /laravel/public$1 last;
...
}
location ~ \.php$ {
internal;
root /home/hamed;
...
}
The above should work (but I have not tested it with your scenario).
The second approach is to use nested location blocks. The location ~ \.php$ { ... } block is then replicated in each application's location block. Something like:
location /blog {
root /home/hamed/www;
...
location ~ \.php$ {
...
}
}
location / {
root /home/hamed/laravel/public;
...
location ~ \.php$ {
...
}
}
Now that one has been tested to work.
Thanks to #RichardSmith I finally managed to create the right configuration. Here is the final working config. I had to use the combination of nested location blocks and an inverse regex match for it to work.
server {
listen 443 ssl;
server_name example.com;
root /home/hamed/laravel/public;
# index index.html index.htm index.php;
ssl_certificate /root/hamed/ssl.crt;
ssl_certificate_key /root/hamed/ssl.key;
location ~ ^/blog(.*)$ {
index index.php;
root /home/hamed/www/;
try_files $uri $uri/ /blog/index.php?do=$request_uri;
location ~ \.php$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
#fastcgi_pass 127.0.0.1:9000;
fastcgi_pass unix:/var/run/php5-fpm.hamed.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
location ~ ^((?!\/blog).)*$ { #this regex is to match anything but `/blog`
index index.php;
root /home/hamed/laravel/public;
try_files $uri $uri/ /index.php?$request_uri;
location ~ \.php$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
#fastcgi_pass 127.0.0.1:9000;
fastcgi_pass unix:/var/run/php5-fpm.hamed.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
error_page 404 /404.html;
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
}
After searching the interweb for hours and none of the "guides" working I turn to you guys and girls for help.
I am attempting to clean all my urls for all files in any directory so www.foo.com/foo.php becomes www.foo.com/foo.
Also I want to tidy up requests such as www.foo.com/foo.php?foo=foo to www.foo.com/foo/foo
Here is my current failed attempt:
server {
# Change these settings to match your machine
listen 80 default_server;
server_name example.com www.example.com;
# Everything below here doesn't need to be changed
access_log /var/log/nginx/example.access.log;
error_log /var/log/nginx/example.error.log;
root /var/www/example.com;
try_files $uri $uri/ #extensionless-php;
index index.html index.htm index.php;
location ~ \.php$ {
include /etc/nginx/fastcgi_params;
fastcgi_pass 127.0.0.1:9000;
# The next two lines should go in your fastcgi_params
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
location #extensionless-php {
rewrite ^(.*)$ $1.php last;
}
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;
}
}
It works but I can still access the old .php url aswell which as far as im aware is bad for SEO? as its duplicate content?
Thanks in Advance
Danny