阿里云Nginx多站点配置指南
阿里云Nginx服务器多站点的配置
今日对多站点环境进行了配置,现将具体操作流程记录如下。
首先定位Nginx的主配置文件,在阿里云环境中,nginx.conf通常位于 /alidata/server/nginx-1.4.4/conf 路径下。
接着,在conf目录下新建一个名为 vhosts 的文件夹,专门用于存放各个独立站点的配置文件。
随后,在nginx.conf文件的末尾添加一行指令:include /alidata/server/nginx/conf/vhosts/*.conf; 以加载该目录下的所有配置。
user www www;
worker_processes 1;
error_log /alidata/log/nginx/error.log crit;
pid /alidata/server/nginx/logs/nginx.pid;
#Specifies the value for maximum file descriptors that can be opened by this process.
worker_rlimit_nofile 65535;
events
{
use epoll;
worker_connections 65535;
}
http {
include mime.types;
default_type application/octet-stream;
#charset gb2312;
server_names_hash_bucket_size 128;
client_header_buffer_size 32k;
large_client_header_buffers 4 32k;
client_max_body_size 8m;
sendfile on;
tcp_nopush on;
keepalive_timeout 60;
tcp_nodelay on;
fastcgi_connect_timeout 300;
fastcgi_send_timeout 300;
fastcgi_read_timeout 300;
fastcgi_buffer_size 64k;
fastcgi_buffers 4 64k;
fastcgi_busy_buffers_size 128k;
fastcgi_temp_file_write_size 128k;
gzip on;
gzip_min_length 1k;
gzip_buffers 4 16k;
gzip_http_version 1.0;
gzip_comp_level 2;
gzip_types text/plain application/x-javascript text/css application/xml;
gzip_vary on;
#limit_zone crawler $binary_remote_addr 10m;
log_format '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
# 加入下面一行 表示将 vhosts 下面所有的 conf 文件包含进来
include /alidata/server/nginx/conf/vhosts/*.conf;
}
接下来,在vhosts目录下编写对应站点的conf配置文件。以下提供一个标准配置范例供参考。
server {
listen 80;
# 这个表示 网站域名, 可以是二级甚至多级域名
server_name localhost demo.com www.demo.com test.demo.com;
# 表示默认索引文件
index index.html index.htm index.php;
# 该站点对应的网站根目录所在
root /alidata/www/demo;
location ~ .*\.(php|php5)?$
{
#fastcgi_pass unix:/tmp/php-cgi.sock;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
include fastcgi.conf;
}
location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
{
expires 30d;
}
location ~ .*\.(js|css)?$
{
expires 1h;
}
# 伪静态规则
include /alidata/server/nginx/conf/rewrite/phpwind.conf;
access_log /alidata/log/nginx/access/phpwind.log;
}
若需增加更多站点,只需复制现有的conf文件,并修改其中的 server_name、root 以及 access_log(视需求而定)等参数即可。
配置完毕后,切勿立即重启Nginx,应先验证配置文件语法是否正确。请进入Nginx的sbin目录,注意区分conf同级目录,避免与其他同名目录混淆。在阿里云默认安装中,该路径通常为 /alidata/server/nginx-1.4.4/sbin。

执行 cd /alidata/server/nginx-1.4.4/sbin 进入目录,然后运行 ./nginx -t 命令。若控制台输出以下两行信息,说明配置成功;否则需根据提示排查错误。
nginx: the configuration file /alidata/server/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /alidata/server/nginx/conf/nginx.conf test is successful

验证通过后,即可重启Nginx服务。在sbin目录下执行 ./nginx -s reload 命令,整个配置过程即告完成。
此外,总结Nginx的几个常用管理命令如下:
启动
./nginx
重启
./nginx -s reload
关闭
ps -ef | grep nginx # 查询nginx主进程号
从容停止:kill -QUIT 主进程号
快速停止:kill -TERM 主进程号
强制停止:kill -9 nginx
若nginx.conf中配置了pid文件路径则使用该路径,否则默认位于logs目录下。
kill -信号类型 '/usr/local/nginx/logs/nginx.pid'
判断配置文件是否正确
./nginx -t
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持本站。