#这里为后端服务器wugk应用集群配置,根据后端实际状况修改便可,tdt_wugk为负载均衡名称,能够任意指定(轮循的方式)php
#但必须跟vhosts.conf虚拟主机的pass段一致,不然不能转发后端的请求。weight配置权重,在fail_timeout内检查max_fails次数,失败则剔除均衡。css
upstream tdt_wugk {html
server 127.0.0.1:8080 weight=1 max_fails=2 fail_timeout=30s;nginx
server 127.0.0.1:8081 weight=1 max_fails=2 fail_timeout=30s;web
}后端
#虚拟主机配置浏览器
server {缓存
#侦听80端口服务器
listen 80;app
#定义使用www.wuguangke.cn访问
server_name www.wuguangke.cn;
#设定本虚拟主机的访问日志
access_log logs/access.log main;
root /data/webapps/wugk; #定义服务器的默认网站根目录位置
index index.php index.html index.htm; #定义首页索引文件的名称
#默认请求
location ~ /{
root /data/www/wugk; #定义服务器的默认网站根目录位置
index index.php index.html index.htm; #定义首页索引文件的名称
#如下是一些反向代理的配置.
proxy_next_upstream http_502 http_504 error timeout invalid_header;
#若是后端的服务器返回502、504、执行超时等错误,自动将请求转发到upstream负载均衡池中的另外一台服务器,实现故障转移。
proxy_redirect off;
#后端的Web服务器能够经过X-Forwarded-For获取用户真实IP
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass http://tdt_wugk; #请求转向后端定义的均衡模块
}
# 定义错误提示页面
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
#配置Nginx动静分离,定义的静态页面直接从Nginx发布目录读取。
location ~ .*\.(html|htm|gif|jpg|jpeg|bmp|png|ico|txt|js|css)$
{
root /data/www/wugk;
#expires定义用户浏览器缓存的时间为3天,若是静态页面不常更新,能够设置更长,这样能够节省带宽和缓解服务器的压力。
expires 3d;
}
#PHP脚本请求所有转发到 FastCGI处理. 使用FastCGI默认配置.
location ~ \.php$ {
root /root;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /data/www/wugk$fastcgi_script_name;
include fastcgi_params;
}
#设定查看Nginx状态的地址
location /NginxStatus {
stub_status on;
}
}
}
1. grep空白行和注释行
# cd /usr/local/nginx/
# grep -v "#" nginx.conf | grep -v "^$" >> nginx.conf.bak
# mv nginx.conf.bak nginx.conf
user nginx nginx;
worker_processes 1;
error_log logs/error.log info;
pid logs/nginx.pid;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
tcp_nopush on;
keepalive_timeout 65;
server {
listen 80;
server_name www.doudou0826.com localhost;
location / {
root html;
index index.html index.htm;
}
error_page 500 502 503 504 /404.html;
location = /50*.html {
root /data/www;
}
}
}
测试:
# /usr/local/nginx/sbin/nginx -t
# /usr/local/nginx/sbin/nginx -s reload
# curl 192.168.119.145
It's Works!
# tail -n 100 /usr/local/nginx/logs/access.log
进入到主页目录下,创建两个目录A、B。分别在A、B目录下创建本身的主页
# cd /usr/local/nginx/html/
# mkdir A B
# cp index.html a/
# cp index.html b/
# cat ./a/index.html
A site ! It's Works!
# cat ./b/index.html
B site ! It's Works!
修改配置文件为:
user nginx nginx;
worker_processes 1;
error_log logs/error.log info;
pid logs/nginx.pid;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
tcp_nopush on;
keepalive_timeout 65;
server {
listen 80;
server_name www.doudou0826a.com;
location / {
root html/a;
index index.html index.htm;
}
}
server {
listen 80;
server_name www.doudou0826b.com;
location / {
root html/b;
index index.html index.htm;
}
}
}
测试:
# /usr/local/nginx/sbin/nginx -t
# /usr/local/nginx/sbin/nginx -s reload
访问以前须要修改本地主机的hosts文件,不然没法用域名访问
# curl www.doudou0826a.com
> A site ! It's Works!
# curl www.doudou0826b.com
B site ! It's Works!
在server里面加入
location /NginxStatus {
stub_status on;
}
例如:
server {
listen 80;
server_name www.doudou0826b.com;
location / {
root html/b;
index index.html index.htm;
}
location /NginxStatus {
stub_status on;
}
}
测试:
# /usr/local/nginx/sbin/nginx -t
# /usr/local/nginx/sbin/nginx -s reload
# curl www.doudou0826b.com/NginxStatusActive connections: 2 server accepts handled requests 45 45 73 Reading: 0 Writing: 1 Waiting: 1