Skip to content

Instantly share code, notes, and snippets.

@geta6
Created June 17, 2012 12:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save geta6/2944405 to your computer and use it in GitHub Desktop.
Save geta6/2944405 to your computer and use it in GitHub Desktop.
Nginxでディレクトリでサブドメインを切りながら共通エラーページを制御します.
:
error_page 404 = @404;
error_page 500 = @500;
error_page 502 = @502;
:
http {
:
upstream 404 { server localhost:404; }
upstream 500 { server localhost:500; }
upstream 502 { server localhost:502; }
:
}
# ------------ #
# DOMAINS #
# ------------ #
server {
listen 80;
charset UTF-8;
server_name example.com
example.net;
rewrite ^ http://www.$host;
}
# ------------ #
# SUBDOMAINS #
# ------------ #
server {
listen 80;
charset UTF-8;
server_name ~^(.+)\.(example\.com)$
~^(.+)\.(example\.net)$;
set $subdomain $1;
set $hostbase $2;
root /var/www/domains/$hostbase/$subdomain/web;
# IF www.example.com THEN ROOT IS /var/www/domain/example.com/www/web/index.php
# IF app.example.net THEN ROOT IS /var/www/domain/example.net/app/web/index.php
access_log /var/log/nginx/domains.a.log;
error_log /var/log/nginx/domains.e.log;
location / {
index index.php index.html;
if (-f $request_filename) {
#expires 7d;
break;
}
if (-f $request_filename/index.php) {
rewrite ^(.*) $uri/index.php last;
break;
}
if (-f $document_root/index.php) {
rewrite ^(.*) /index.php last;
break;
}
}
location @404 {
include /etc/nginx/conf.d/proxy.conf;
proxy_pass http://404;
}
location @500 {
include /etc/nginx/conf.d/proxy.conf;
proxy_pass http://500;
}
location @502 {
include /etc/nginx/conf.d/proxy.conf;
proxy_pass http://502;
}
location ~* \.php$ {
fastcgi_pass localhost:9000;
include /etc/nginx/conf.d/fastcgi.conf;
}
}
# ------------ #
# NODE SERVER #
# ------------ #
upstream node {
server localhost:3000;
}
server {
listen 80;
charset UTF-8;
server_name node.example.com;
location @404 {
include /etc/nginx/conf.d/proxy.conf;
proxy_pass http://404;
}
location @500 {
include /etc/nginx/conf.d/proxy.conf;
proxy_pass http://500;
}
location @502 {
include /etc/nginx/conf.d/proxy.conf;
proxy_pass http://502;
}
location / {
include /etc/nginx/conf.d/proxy.conf;
proxy_pass http://node;
}
}
# ------------ #
# ERROR SERVER #
# ------------ #
server {
listen 404;
listen 500;
listen 502;
root /var/www/apps/error;
location / {
index index.php;
if ($server_port = 404) {
rewrite ^.* /index.php?e=404 last;
return 404;
}
if ($server_port = 500) {
rewrite ^.* /index.php?e=500 last;
return 500;
}
if ($server_port = 502) {
rewrite ^.* /index.php?e=502 last;
return 502;
}
}
location ~* \.php$ {
fastcgi_pass localhost:9000;
include /etc/nginx/conf.d/fastcgi.conf;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment