Skip to content

Instantly share code, notes, and snippets.

@ChrisMcKee
Created October 12, 2011 10:06
Show Gist options
  • Save ChrisMcKee/1280806 to your computer and use it in GitHub Desktop.
Save ChrisMcKee/1280806 to your computer and use it in GitHub Desktop.
NGINX Recipes
### Have to admit at this point, as "fast" as development with cake is... I hate the bastard.
server {
listen 80;
server_name www.domain.com;
rewrite ^/(.*) http://domain.com/$1 permanent;
}
server {
listen 80;
server_name domain.com;
access_log /var/www/logs/domain.com.access.log;
error_log /var/www/logs/domain.com.error.log;
## Default location
location / {
root /var/www/vhost/domain.com/htdocs/;
index index.php index.html index.htm;
add_header Vary Accept-Encoding;
if (-f $request_filename) {
break;
}
if (-d $request_filename) {
break;
}
}
# that's for cakephp
location /cakeapp {
rewrite ^/cakeapp$ cakeapp/ permanent;
rewrite ^/cakeapp/(.+)$ /$1 break;
root /var/www/vhost/domain.com/htdocs/cakeapp$
try_files $uri $uri/ @cakephp;
}
#PHP
location ~ \.php$ {
include /etc/nginx/fastcgi.conf; #or whatever you named it
fastcgi_param SCRIPT_FILENAME /var/www/vhost/domain.com/htdocs$$
fastcgi_pass 127.0.0.1:9000;
}
# that's for cakephp execution
location @cakephp {
set $q $request_uri;
if ($request_uri ~ "^/cakeapp(.+)$") {
set $q $1;
}
fastcgi_param SCRIPT_FILENAME /var/www/vhost/domain.com/$
fastcgi_param QUERY_STRING url=$request_uri;
fastcgi_pass 127.0.0.1:9000;
}
# that's for cakephp execution
location @cakephp {
set $q $request_uri;
if ($request_uri ~ "^/cakeapp(.+)$") {
set $q $1;
}
fastcgi_param SCRIPT_FILENAME /var/www/vhost/domain.com/$
fastcgi_param QUERY_STRING url=$request_uri;
fastcgi_pass 127.0.0.1:9000;
include /etc/nginx/fastcgi_params;
}
#Fake FavIcon
location = /favicon.ico {
#empty_gif;
return 204;
}
## Disable viewing .htaccess & .htpassword
location ~ /\.ht {
deny all;
}
}
server {
listen 80;
server_name couchdb.domain.com;
location / {
proxy_pass http://127.0.0.1:5984;
proxy_redirect off;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
location ~ ^/(.*)/_changes {
proxy_pass http://127.0.0.1:5984;
proxy_redirect off;
proxy_buffering off;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
server {
## YOU ONLY NEED THIS BIT IF YOU WANT TO REDIRECT www.domain to domain
listen 80;
server_name www.domain.com;
rewrite ^/(.*) http://domain.com/$1 permanent;
}
server {
listen 80;
server_name domain.com;
access_log /var/www/vhost/domain.com.access.log;
error_log /var/www/vhost/domain.com.error.log;
## Default location
location / {
root /var/www/vhost/domain.com/htdocs/;
index index.php index.html index.htm;
add_header Vary Accept-Encoding;
include /etc/nginx/wordpress_params.regular;
}
## Images and static content is treated different
location ~* ^.+.(jpg|jpeg|gif|png|ico)$ {
access_log off;
gzip off;
expires 30d;
root /var/www/vhost/domain.com/htdocs;
}
## JS + CSS
location ~* ^.+.(css|js|xml|eot|woff|svg|css)$ {
access_log off;
expires 8d;
gzip on;
add_header Cache-Control public;
add_header Vary Accept-Encoding;
root /var/www/vhost/domain.com/htdocs;
}
location ~ \.php$ {
include /etc/nginx/fastcgi.conf; #or whatever you named it
fastcgi_param SCRIPT_FILENAME /var/www/vhost/domain.com/htdocs$fastcgi_script_name;
fastcgi_pass 127.0.0.1:9000;
}
## Disable viewing .htaccess & .htpassword
location ~ /\.ht {
deny all;
}
}
####### wordpress_params.regular BEGINS HERE
# WordPress pretty URLs:
# this serves static files that exist without running other rewrite tests
if (-f $request_filename) {
expires 30d;
break;
}
if (-d $request_filename) {
break;
}
# this sends all non-existing file or directory requests to index.php
if (!-e $request_filename) {
rewrite ^(.+)$ /index.php?q=$1 last;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment