I'm trying to make the urls for nginx (Ubuntu 14.04) clean, instead of example.com/profile.php it would be example.com/profile, or example.com/about.html to example.com/about
I have tried pretty much all the other ones on stackoverflow however they do not work, including making the site have a 500 internal server error, to making me download the PHP file
Current config
server {
listen 443 ssl;
root /usr/share/nginx/html;
index index.php index.html index.htm;
include /etc/nginx/mime.types;
server_name mydomain.com;
ssl_certificate /etc/letsencrypt/live/mydomain.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/mydomain.com/privkey.pem;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_prefer_server_ciphers on;
ssl_ciphers 'EECDH+AESGCM:EDH+AESGCM:AES256+EECDH:AES256+EDH';
location / {
try_files $uri.html $uri $uri/ =404;
}
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;
}
}
server {
listen 80;
server_name mydomain.com;
return 301 https://$host$request_uri;
}
server {
listen 443;
server_name api.mydomain.com;
location / {
proxy_pass https://mydomain:1337;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_connect_timeout 150;
proxy_send_timeout 100;
proxy_read_timeout 100;
proxy_buffers 4 32k;
client_max_body_size 8m;
client_body_buffer_size 128k;
}
}
server {
listen 1337 ssl;
server_name api.mydomain.com;
root /usr/share/nginx/api;
ssl_certificate /etc/letsencrypt/live/mydomain.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/mydomain.com/privkey.pem;
}
Use the try_files
[1] directive.
location / {
try_files $uri.php $uri.html =404;
}
In this example, if the browser was requesting /folder/file
, it would first try to find file.php
and then file.html
before giving up and throwing a 404 error.
If that doesn't work, you can use an if statement instead:
location / {
if (-f $request_filename.php) {
rewrite ^ $uri.php;
break;
}
if (-f $request_filename.html) {
rewrite ^ $uri.html;
break;
}
}