4. Nginx配置文件二及虚拟主机配置

#这里为后端服务器wugk应用集群配置,根据后端实际情况修改即可,tdt_wugk为负载均衡名称,可以任意指定轮循的方式

#但必须跟vhosts.conf虚拟主机的pass段一致,否则不能转发后端的请求。weight配置权重,在fail_timeout内检查max_fails次数,失败则剔除均衡。

upstream tdt_wugk {

server   127.0.0.1:8080 weight=1 max_fails=2 fail_timeout=30s;

server   127.0.0.1:8081 weight=1 max_fails=2 fail_timeout=30s;

}

   #虚拟主机配置

server {

#侦听80端口

        listen       80;

        #定义使用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;

  #如果后端的服务器返回502504、执行超时等错误,自动将请求转发到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

  1. 配置一个页面

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

 

实例二:配置一个简单的虚拟主机

进入到主页目录下,建立两个目录AB。分别在AB目录下建立自己的主页

# 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!

实例三:设定查看Nginx状态的地址

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/NginxStatus
Active connections: 2
server accepts handled requests
 45 45 73
Reading: 0 Writing: 1 Waiting: 1

 

posted @ 2017-11-27 16:59  ling小龙  阅读(200)  评论(0编辑  收藏  举报