我的Nginx分开式配置文件管理方案

用过debian上的apache2就羡慕debian上面apache2的配置文件管理可谓实在是太方便了
想着自己编译的nginx也能有这么方便就好了,于是动手改造
我的nginx是编译到/usr/local/nginx/的
(编译参数./configure --with-ipv6 --with-http_ssl_module --with-mail_ssl_module --with-http_stub_status_module),
配置文件在/usr/local/nginx/conf。下面开始分开式改造。
首先换到/usr/local/nginx/conf/目录,编辑切nginx.conf,注释所有站点server配置设置,在会后一节大括号前加入这样一句话

include sites-enabled/*;

创建目录

[bash]mkdir sites-enabled
mkdir sites-available[/bash]

在sites-available里面创建一个默认http站点default和一个默认https站点defaultssl

server {
#同时监听V6/V4 并且作为默认主机
listen 80 default;
listen [::]:80 default;

#主机头
server_name localhost;

#编码
#charset koi8-r;

#日志配置
access_log logs/default.access.log main;
error_log logs/default.error.log;

#首页配置
location / {
root html;
index index.html index.htm;
}

#错误代码
#error_page 404 /404.html;

# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}

#公共配置
include common_setting;
}

server {
#同时监听V6/V4 并且作为默认主机
listen 443 default;
listen [::]:443 default;

#主机头
server_name localhost;

#编码
#charset koi8-r;

#SSL证书配置
ssl on;
ssl_certificate ssl//py/server.crt;
ssl_certificate_key ssl/py/server.key;

ssl_session_timeout 5m;

ssl_protocols SSLv3 TLSv1; #SSLv2
ssl_ciphers ALL:!ADH:!EXPORT56:RC4+RSA:+HIGH:+MEDIUM:+LOW:+SSLv2:+EXP;
ssl_prefer_server_ciphers on;

#日志配置
access_log logs/defaultssl.access.log main;
error_log logs/defaultssl.error.log;

#首页配置
location / {
root html;
index index.html index.htm;
}

#错误代码
#error_page 404 /404.html;

# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}

#公共配置
include common_setting;
}

common_settings是位于conf目录的一个公共配置文件,记录了一些公共的配置,比如图片和js的过期时间

location ~* \.(gif|jpg|jpeg|png|bmp|swf|js|css)$
{
expires 31d;
}

这样默认站点就配置好了。现在开始写ngensite/dissite脚本

[bash]vim /usr/local/bin/ngensite[/bash]

[bash]
#!/bin/sh
cd /usr/local/nginx/conf/sites-enabled
sites_available="ln -s ../sites-available/"

[ $# == 0 ] && echo Specify A Site Name ,Please! && exit 1
[ "$1" == "all" ] && ${sites_available}/* ./ >/dev/null 2>&1
[ "$1" == "all" ] && echo Enabled all sites && exit 0
[ -e $1 ] && echo Error,Site $1 already enabled! && exit 1
[ -e ../sites-available/$1 ] && ${sites_available}/$1 ./ && echo Site $1 Enabled ,Use \"Nginx -s reload\" to Take Effect ! && exit 0
echo Error,Site $1 Not Enabled!
[/bash]

[bash]vim /usr/local/bin/ngdissite[/bash]

[bash]#!/bin/sh
cd /usr/local/nginx/conf/sites-enabled
#sites_available="ln -s ../sites-available/"

[ $# == 0 ] && echo Specify A Site Name ,Please! && exit 1
[ "$1" == "all" ] && rm -f ls |grep -v default >/dev/null 2>&1 && echo Disabled all sites && exit 0
[ ! -e $1 ] && echo Error,Site $1 already Disabled! && exit 1
rm -f $1 && echo Site $1 Disabled ,Use \"Nginx -s reload\" to Take Effect ! && exit 0
[/bash]

[bash]chmod +x /usr/local/bin/ng*[/bash]

用法
ngensite
all 允许所有站点
site.config.name 允许名字为site.config.name配置文件内指定的虚拟主机
ngdissite
all 禁止所有站点(除default站点外)
site.config.name 禁止名字为site.config.name配置文件内指定的虚拟主机

创建一个common_server,这个common_server位于conf目录下面,记录了公共的虚拟主机配置

#Server根目录
root $rootdir;

#FastCGI配置
location ~* \.php$ {
#不存在的文件返回404
if (!-e $request_filename) {
return 404;
}
fastcgi_pass unix:/var/run/www.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $rootdir/$fastcgi_script_name;
include fastcgi_params;
}

location ~ /\.ht {
deny all;
}

location ~* /\.svn {
return 404;
}

#错误代码
#error_page 404 /404.html;

# redirect server error pages to the static page /50x.html
#

error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}

#载入公共配置
include common_setting;

common_server末尾去调用common_setting
编辑fastcgi_params
末尾加上

#让Nginx接管FastCGI运行错误
fastcgi_intercept_errors on;
#判断是否是SSL站点

set $is_https off;

if ($scheme = "https") {
set $is_https on;
}
fastcgi_param HTTPS $is_https;

要修改任何涉足虚拟主机配置的公共设置,编辑common_server即可一次完成,需要增加任何公共配,编辑common_setting即可一次完成

这样,一会新建虚拟主机,只要写短短的一段就可以了
创建一个虚拟主机文件

server {
#同时监听V6和V4地址
listen 80;
listen [::]:80;
#listen 443;
#listen [::]:443;

#私有变量
set $domain bbs.ooxx.com;
set $rootdir /var/web/bbs.ooxx.com;

server_name bbs.ooxx.com;

access_log logs/bbs.ooxx.com.access.log main;
error_log logs/bbs.ooxx.com.error.log;

#ssl on;
#ssl_certificate ssl//py/server.crt;
#ssl_certificate_key ssl/py/server.key;

#ssl_session_timeout 5m;

#ssl_protocols SSLv3 TLSv1; #SSLv2
#ssl_ciphers ALL:!ADH:!EXPORT56:RC4+RSA:+HIGH:+MEDIUM:+LOW:+SSLv2:+EXP;
#ssl_prefer_server_ciphers on;

#首页配置
location / {
index index.html index.htm index.php;
}

#载入公共服务器配置

include common_server;
}


Tips:

1.需要注意的是有个系统参数对nginx的服务有影响。

如果你默认那样设置,ngixn的log里面,你的IPV4地址会被nginx添加多余的前缀。这样运行的php程序基于IPV4的校验就会出错需要这样

[bash]echo 1 > /proc/sys/net/ipv6/bindv6only [/bash]

,然后再启动nginx。
不修改这个参数的话用listen [::]:80就可以同时监听IPV6和IPV4地址,但是那样的副作用就如上面所说,会被添加::ffff:的前缀。
修改了就必须同时监听两个端口才可以。

listen 80
listen [::]:80

2.确定nginx的工作模式
没有使用epoll的nginx还不如apache,可以空下面的shell确定nginx的工作模式

[bash]for pid in ps aux |grep nginx|grep worker |awk '{print $2}'
do
ls -l /proc/$pid/fd/ |grep poll
done[/bash]

看见epoll或者anon_inode:[eventpoll]就表示nginx工作在epoll模式下


Update1:
2011年2月24日 Nginx添加虚拟主机脚本

[bash]vim /usr/local/bin/ngaddsite[/bash]

[bash]#!/bin/sh
#by [email protected]
#2011年2月24日 16:06:52
#结尾不要有"/"

dir=/usr/local/nginx/conf/sites-available
webdir=/var/web/$1
logdir=logs
nginxroot=/usr/local/nginx

[ "$1" = "" ] && echo You Must Specify a Vaild Domain Nmae ! && exit 1

cd $dir

[ -e $1 ] && echo $1 Existed! && exit 2

cat <<EOF >$1
server {
#同时监听V6和V4地址
listen 80;
listen [::]:80;
#listen 443;
#listen [::]:443;

#私有变量
set \$domain $1;
set \$rootdir $webdir;

#主机头
server_name $1;

#日志配置
access_log $logdir/$1/$1.access.log main;
error_log $logdir/$1/$1.error.log;

#SSl配置
#ssl on;
#ssl_certificate ssl/py/server.crt;
#ssl_certificate_key ssl/py/server.key;

#ssl_session_timeout 5m;

#ssl_protocols SSLv3 TLSv1; #SSLv2
#ssl_ciphers ALL:!ADH:!EXPORT56:RC4+RSA:+HIGH:+MEDIUM:+LOW:+SSLv2:+EXP;
#ssl_prefer_server_ciphers on;

#首页配置
location / {
index index.html index.htm index.php;
}

#载入公共服务器配置

include common_server;
}
EOF

if [ "dirname $logdir" == "." ];then
mkdir -p $nginxroot/$logdir/$1/
else
mkdir -p $logdir/$1/
fi

nano $1

echo done![/bash]

Author Info :
  • From:我的Nginx分开式配置文件管理方案
  • URL:https://blog.ihipop.com/2011/02/2076.html
  • Please Reserve This Link,Thanks!
  • 《我的Nginx分开式配置文件管理方案》上有7条评论

    1. @tony
      你的ROOT不对 要么你干脆把root写到server节里面 不要放在location节里面 要么干脆在php的location里面重新声明一遍。
      这两天我又总结了一下 下次再发一个更加灵活的方案上来~

    回复 ihipop 取消回复

    您的电子邮箱地址不会被公开。 必填项已用*标注