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;
}
}
http://nginx.org/en/docs/http/ngx_http_core_module.html#try_files
Related
I have trouble to run davical (php) web calendar. There is no errol log in nginx error logs. When is calendar under \ location everything work. But when i have calendar under /calendar location. it returns 404.
default server root is: /usr/share/nginx/html/default
calendar index.php path: /usr/share/nginx/html/calendar/davical/htdocs\index.php
os: Centos 7
server {
listen 80 default_server;
server_name my_domain_name;
return 301 https://$server_name$request_uri;
}
Https
server {
listen 443 ssl http2;
server_name my_domain_name;
fastcgi_buffers 8 16k;
fastcgi_buffer_size 32k;
fastcgi_connect_timeout 300;
fastcgi_send_timeout 300;
fastcgi_read_timeout 300;
ssl on;
ssl_certificate "/etc/pki/tls/certs/nginx/certificate.pem";
ssl_certificate_key "/etc/pki/tls/certs/nginx/privatekey.pem";
ssl_session_cache shared:SSL:10m;
ssl_session_timeout 10m;
ssl_session_tickets off;
ssl_dhparam "/etc/pki/tls/certs/nginx/dhparam.pem";
ssl_prefer_server_ciphers on;
ssl_protocols TLSv1.1 TLSv1.2;
ssl_ciphers HIGH:!aNULL:!MD5;
resolver 8.8.8.8 8.8.4.4;
ssl_stapling on;
ssl_stapling_verify on;
ssl_trusted_certificate "/etc/pki/tls/certs/nginx/certificate.pem";
add_header Strict-Transport-Security "max-age=31536000;includeSubdomains; preload";
root /usr/share/nginx/html/default;
index index.php index.html index.htm;
include /etc/nginx/default.d/php-fpm.conf;
location /calendar {
alias /usr/share/nginx/html/calendar/davical/htdocs;
}
error_page 404 /404.html;
location = /40x.html {
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
}
}
php-fpm.conf
location ~ \.php$ {
try_files $uri =404;
fastcgi_param HTTPS on;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME
$document_root$fastcgi_script_name;
include fastcgi_params;
}
Your existing location ~ \.php$ block serves the /usr/share/nginx/html/default root. You need a nested location to process PHP files under the /calendar URI.
Assuming that your calendar app is designed to work within a subfolder, this may work for you:
location ^~ /calendar {
alias /usr/share/nginx/html/calendar/davical/htdocs;
index index.php;
if (!-e $request_filename) {
rewrite ^ /calendar/index.php last;
}
location ~ \.php$ {
if (!-f $request_filename) { return 404; }
include fastcgi_params;
fastcgi_pass 127.0.0.1:9000;
fastcgi_param SCRIPT_FILENAME $request_filename;
}
}
Use the ^~ modifier to prevent the other location ~ \.php$ block from taking precedence (see this document for more). Use $request_filename, as it works with alias. Avoid using try_files with alias (see this issue).
I am able to run node on port 8080 with pointing it to domain.com but I want to run php alongside of it.
Sample configuration is as below:
proxy_buffering on;
proxy_buffer_size 1k;
proxy_buffers 24 4k;
proxy_busy_buffers_size 8k;
proxy_max_temp_file_size 2048m;
proxy_temp_file_write_size 32k;
server {
listen 80;
server_name domain.com;
location / {
proxy_pass http://domain.com:8080;
proxy_redirect off;
proxy_set_header X-Forwarded-For $remote_addr;
location ~* \.(html|css|jpg|gif|ico|js)$ {
proxy_cache cache;
proxy_cache_key $host$uri$is_args$args;
proxy_cache_valid 200 301 302 30m;
expires 30m;
proxy_pass http://domain.com:8080;
}
}
}
Now I want to run domain.com/php as php server so any request which has domain.com/php will process php and others will work on node. Is it possible?
server {
listen 80;
server_name www.domain.com;
root /var/www/html/testphp/api;
location / {
proxy_pass http://www.domain.com:8080;
proxy_redirect off;
proxy_set_header X-Forwarded-For $remote_addr;
location ~* \.(html|css|jpg|gif|ico|js)$ {
proxy_cache cache;
proxy_cache_key $host$uri$is_args$args;
proxy_cache_valid 200 301 302 30m;
expires 30m;
proxy_pass http://www.domain.com:8080;
}
}
location /api/ {
alias /var/www/html/testphp/api/;
try_files $uri $uri/ /api/index.php;
location ~ \.php$ {
include /etc/nginx/fastcgi_params;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_index index.php;
fastcgi_pass unix:/run/php/php5.6-fpm.sock;
fastcgi_param SCRIPT_FILENAME $request_filename;
}
}
}
I know there are a lot of threads about this topic but none seemed to work for me. I am not sure if it is because I have to do it to both server blocks, or I was doing something wrong. Please help me out.
Below is my nginx config on the remote server on Amazon, the first block represent the backend and the second block represent the frontend:
limit_conn_zone $binary_remote_addr zone=conn_limit_per_ip:60m; # nginx 1.1.9 or higher
limit_req_zone $binary_remote_addr zone=req_limit_per_ip:60m rate=20r/s;
server {
listen 8080;
server_name api.commonskudev.com;
root /var/www/api/public;
gzip on;
server_tokens off;
index index.php;
client_max_body_size 64M;
fastcgi_read_timeout 300;
fastcgi_send_timeout 300;
fastcgi_buffers 16 16k;
fastcgi_buffer_size 32k;
send_timeout 300;
try_files $uri $uri/ #rewrite-staging;
location #rewrite-staging {
rewrite ^ /index.php;
}
location ~* \.php$ {
include fastcgi_params;
# fastcgi_param HTTPS on;
fastcgi_pass 127.0.0.1:9000;
}
}
server {
listen 443 default_server ssl;
server_name rightsleeve.commonskudev.com;
# rewrite ^ http://commonskudev.com/maintenance.html;
ssl on;
# ssl_certificate /etc/ssl/certs/ssl-cert-snakeoil.pem;
# ssl_certificate_key /etc/ssl/private/ssl-cert-snakeoil.key;
ssl_certificate /etc/nginx/csku-dev.crt;
ssl_certificate_key /etc/nginx/csku-dev.key;
gzip on;
gzip_proxied any;
client_max_body_size 64M;
fastcgi_read_timeout 300;
fastcgi_send_timeout 300;
fastcgi_buffers 16 16k;
fastcgi_buffer_size 32k;
send_timeout 300;
root /var/www/web;
server_tokens off;
index index.php;
error_page 404 /404.php;
rewrite ^/v[0-9]+\.[0-9]+/(.*)$ /$1;
rewrite ^/project/([0-9]+) /project.php?job_number=$1;
if ($http_referer ~* "semalt\.com") {
return 444;
}
location ~* ^(css|js|images|files) {
expires 1y;
add_header Pragma public;
add_header Cache-Control public;
}
location ~* \.(ttf|woff) {
add_header Access-Control-Allow-Origin "*";
}
location ~* \.php$ {
if (!-f $document_root/$fastcgi_script_name) {
return 404;
}
limit_conn conn_limit_per_ip 35;
limit_req zone=req_limit_per_ip burst=35;
include fastcgi_params;
fastcgi_param HTTPS on;
fastcgi_pass 127.0.0.1:9000;
}
location ~* \.(png|jpg|dst|xls) {
try_files $uri $uri/ #nofile;
}
location #nofile {
rewrite ^ /images/404.png;
}
if ($uri ~* ^/([a-zA-Z0-9_\-]+)$) {
rewrite ^/([a-zA-Z0-9_\-]+) /vanity_url.php?mask=$1&$args;
}
location /v1 {
proxy_pass http://api.commonskudev.com:8080;
proxy_set_header Host $host;
}
}
server {
listen 80 default_server;
server_name rightsleeve.commonskudev.com;
rewrite ^(.*) https://$host$1 permanent;
}
You can use a rewrite similar to this:
location / {
try_files $uri $uri.html $uri/ #extensionless-php;
index index.html index.htm index.php;
}
location ~ \.php$ {
try_files $uri =404;
}
location #extensionless-php {
rewrite ^(.*)$ $1.php last;
}
As taken from this SO answer.
Use this:
server {
listen 80;
server_name www.example.local;
root /var/www/vhosts/example/httpdocs;
index index.html index.htm index.php;
location / {
try_files $uri $uri/ #ext;
}
location ~ \/\.php {
rewrite "^(.*)\/.php" $1.php last;
}
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/run/php/php7.4-fpm.sock;
}
location #ext {
rewrite "^(.*)$" $1.php;
}
}
I am successfully running a Wordpress installation alongside Meteor using NGINX. I have no real experience with Wordpress or php, so this may be a simple fix.
The following configuration works:
server_tokens off;
map $http_upgrade $connection_upgrade {
default upgrade;
'' close;
}
upstream mydomain-production {
server localhost:8000;
}
# redirect all non-subdomain traffic to https
server {
listen 80;
server_name www.mydomain.com mydomain.com;
rewrite ^ https://$host$request_uri? permanent;
access_log /var/log/nginx/mydomain.access.log;
error_log /var/log/nginx/mydomain.error.log;
}
# this non-subdomain serves meteor correctly
server {
listen 443 ssl spdy;
server_name www.mydomain.com mydomain.com;
access_log /var/log/nginx/mydomain.secure.access.log;
error_log /var/log/nginx/mydomain.secure.error.log debug;
ssl_certificate #...removed...# ;
ssl_certificate_key #...removed...# ;
ssl_stapling on;
ssl_session_cache shared:SSL:10m;
ssl_session_timeout 5m;
ssl_prefer_server_ciphers on;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_ciphers #...removed...# ;
add_header Strict-Transport-Security "max-age=31536000;";
################################
# additional code will go here #
################################
location / {
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarder-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_set_header X-NginX-Proxy true;
proxy_pass http://mydomain-production;
proxy_redirect off;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
if ($uri != '/') {
expires 30d;
}
}
}
# this temporary subdomain serves wordpress correctly
server {
listen 80;
server_name wordpress.mydomain.com;
access_log /var/log/nginx/mydomain.access.log;
error_log /var/log/nginx/mydomain.error.log;
index index.php index.html index.htm;
root /var/www;
location / {
try_files $uri $uri/ /index.php?q=$uri&$args;
}
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 unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_intercept_errors on;
include fastcgi_params;
}
}
So since I have Wordpress functioning on a temporary subdomain, I want to make it work on the same domain as Meteor and include location directives to route certain requests to Wordpress instead.
I tried adding the following to the 443 server name:
# additional code
location /blog {
root /var/www;
try_files $uri $uri/ /index.php?q=$uri&$args;
index index.php index.html index.htm;
}
location /wp-admin {
root /var/www;
try_files $uri $uri/ /index.php?q=$uri&$args;
index index.php index.html index.htm;
}
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;
fastcgi_intercept_errors on;
include fastcgi_params;
}
After doing this, I get an NGINX 404 page at mydomain/blog. So the location directive is successfully sending the request to /var/www instead of Meteor, but for some reason it is not getting to the Wordpress router. I have linked my NGINX error debug output here.
This was somehow solved by simply moving the root /var/www; and index index.php index.html index.htm; outside of the location directive(s). I would be interested to know why this is necessary if anyone can shed any light on this.
I'm trying to set node.js app on main domain and php-based forum on subdomain. Node.JS app works on 8000 port. Here's my config:
server {
listen 80;
listen [::]:80 default_server ipv6only=on;
server_name myawesomeapp.ru;
location / {
proxy_pass http://127.0.0.1:8000;
access_log off;
}
location ~* ^.+\.(jpg|jpeg|gif|png|ico|css|zip|tgz|gz|rar|bz2|pdf|txt|tar|wav|bmp|rtf|js|flv|swf|html|htm)$ {
root /srv/myawesomeapp/static;
}
}
server {
listen 80;
listen [::]:80 default_server ipv6only=on;
root /usr/share/nginx/html;
index index.php index.html index.htm;
server_name forum.myawesomeapp.ru
location / {
try_files $uri $uri/ /index.html;
}
error_page 404 /404.html;
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
location ~ \.php$ {
#fastcgi_pass 127.0.0.1:9000;
# With php5-fpm:
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
Both node.js app & php forum can't be reached at myawesomeapp.ru. 127.0.0.1:8000 shows nodejs-app. What's wrong with my config? Thanks.
p.s. my php files are placed in /usr/share/nginx/html
Please include any messages you see on response of trying to visit both vhosts. As well make sure you include this setup in your nginx config as well as service nginx reload after changing configurations.
In order to proxy nginx to node you have to use upstreams. Here is configuration that might suit your needs:
upstream node {
server 127.0.0.1:8000;
}
server {
listen 80;
server_name myawesomeapp.ru;
location ~* ^.+\.(jpg|jpeg|gif|png|ico|css|zip|tgz|gz|rar|bz2|pdf|txt|tar|wav|bmp|rtf|js|flv|swf|html|htm)$ {
access_log off;
root /srv/myawesomeapp/static
try_files $uri $uri/ =404;
expires 365d;
}
location / {
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_set_header X-NginX-Proxy true;
proxy_pass http://node/;
proxy_redirect off;
}
}
For your forum try this config:
server {
server_name www.forum.myawesomeapp.ru;
rewrite ^(.*) http://forum.myawesomeapp.ru$1 permanent;
}
server {
listen 80 default_server;
server_name forum.myawesomeapp.ru;
root /usr/share/nginx/html;
index index.php;
charset utf-8;
gzip on;
gzip_disable "MSIE [1-6]\.(?!.*SV1)";
location ~* ^.+\.(jpg|jpeg|gif|png|ico|css|zip|tgz|gz|rar|bz2|pdf|txt|tar|wav|bmp|rtf|js|flv|swf|html|htm)$ {
access_log off;
try_files $uri $uri/ =404;
expires 365d;
}
error_page 404 /404.html;
location ~ \.php$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info;
fastcgi_intercept_errors on;
}
}
Try just remove this lines from your config:
listen [::]:80 default_server ipv6only=on;