还没有安装Nginx环境的先看《Centos 6配置安装Nginx Web环境》介绍;
本篇记录Nginx如何设置多站点运行方法,亲测可行,方法简单,注意细节不弄错很快就能搞定多站点的配置,只要服务器受得了想要多少个站点就多少个站点没啥限制;
第一步:创建vhosts目录
mkdir /etc/nginx/vhosts
vhosts目录是用来存放不同站点.conf配置文件的(比如站点对应的域名,网站存放的目录,日志目录等等)
第二步:/etc/nginx/vhosts/ 目录下创建img.5yun.org.conf配置文件
server { listen 80; server_name img.5yun.org; access_log /www/access_img.5yun.org main; location / { root /www/v.5yun.org; index index.php index.html index.htm; } 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; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME /www/img.5yun.org/$fastcgi_script_name; include fastcgi_params; } location ~ /.ht { deny all; } }
代码第3行就是需要绑定的域名
代码第8行就是我把该站点存放在www目录下的img.5yun.org文件夹之中
有多少个站点就建立多少个.conf配置文件存放在/etc/nginx/vhosts/ 文件夹下
第三步:编辑/etc/nginx/nginx.conf
user nginx; worker_processes 1; # main server error log error_log /var/log/nginx/error.log ; pid /var/run/nginx.pid; events { worker_connections 1024; } # main server config http { include mime.types; default_type application/octet-stream; log_format main '$remote_addr - $remote_user [$time_local] $request ' '"$status" $body_bytes_sent "$http_referer" ' '"$http_user_agent" "$http_x_forwarded_for"'; sendfile on; #tcp_nopush on; #keepalive_timeout 0; keepalive_timeout 65; gzip on; server { listen 80; server_name _; access_log /var/log/nginx/access.log main; server_name_in_redirect off; location / { root /usr/share/nginx/html; index index.html; } } # 包含所有的虚拟主机的配置文件 include /etc/nginx/vhosts/*.conf; }
第38行才是重点,路径不能搞错不然解析识别不到第二步中我们设置的站点配置文件;
最后一步:重启Nginx
/etc/init.d/nginx restart
重启完后,Nginx多站点设置的对应多站点也就正常运行了;
转载请注明:有客帮 » Centos 6配置安装Nginx多站点方法