Nginx服务器(负载均衡)

来源:互联网 发布:深圳聚宝网络 编辑:程序博客网 时间:2024/06/10 01:18
1.首先创建个文件

vi /etc/yum.repos.d/nginx.repo

 

2.复制下面文本,保存并退出

[nginx]

name=nginx repo

baseurl=http://nginx.org/packages/centos/$releasever/$basearch/

gpgcheck=0

enabled=1

3.运行以下命令进行安装

#yum install pcre  安装依赖包

#yum install nginx 

4.创建日子文件
 vi 
/etc/nginx/logs/status.log
  

5.配置
nginx.conf文件
 
 

 
 
#user  nobody;
worker_processes  2;
#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;
#pid        logs/nginx.pid;
events {
    worker_connections  65535;
}
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"';
    #access_log  logs/access.log  main;
    sendfile        on;
    #tcp_nopush     on;
    #keepalive_timeout  0;
    keepalive_timeout  65;
    #gzip  on;
    server {
    listen       80;
        server_name  www.17bibi.com;
        charset utf-8;
        index index.html index.htm index.jsp login.jsp;
        #access_log  logs/host.access.log  main;
        root /var/www/shop;
        location /
        {
            index index.jsp;
            proxy_pass http://localhost:7001;
            proxy_set_header        Host $host;
            proxy_set_header        X-Real-IP $remote_addr;
            proxy_set_header        X-Forwarded-For $proxy_add_x_forwarded_for;
        }
        location ~.*\.(gif|jpg|jpeg|png|bmp|swf|html)$
        {
            proxy_redirect off;
            proxy_set_header Host $host;
            proxy_cache_valid 200 302 1h;
            proxy_cache_valid 301 1d;
            proxy_cache_valid any 1m;
            expires 30d;
        }
        location ~.*\.(js|css)?$
        {
            expires 1h;
        }
        location ~(favicon.ico)
        {
            log_not_found off;
            expires 30d;
            break;
        }
        location /status
        {
            access_log /etc/nginx/logs/status.log;
            auth_basic "NginxStatus";
            allow 10.0.209.0/24;
            deny all;
       }
        error_page  404              /404.html;
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }
    include /etc/nginx/vhosts/*; 
}
 


 5.启动nginx
 启动:service nginx start

停止:service nginx stop

重启:service nginx reconfigure

查看状态:service nginx status


6.配置多个子域名
在配置文件最后面加上配置指向
 include /usr/local/nginx/conf/vhosts/*;  

#vi /usr/local/nginx/conf/vhosts/test.17bibi.com.conf
 子域名的配置
server {
        listen       80;
        server_name  test.17bibi.com;
        charset utf-8;
        index index.html index.htm index.jsp login.jsp;
        #access_log  logs/host.access.log  main;
        root /var/www/shop2/;
        location /
        {
            index index.jsp;
            proxy_pass http://localhost:7070;
            proxy_set_header        Host $host;
            proxy_set_header        X-Real-IP $remote_addr;
            proxy_set_header        X-Forwarded-For $proxy_add_x_forwarded_for;
        }
        location ~.*\.(gif|jpg|jpeg|png|bmp|swf|html)$
        {
            proxy_redirect off;
            proxy_set_header Host $host;
            proxy_cache_valid 200 302 1h;
            proxy_cache_valid 301 1d;
            proxy_cache_valid any 1m;
            expires 30d;
        }
        location ~.*\.(js|css)?$
        {
         expires 1h;
        }
      location ~(favicon.ico) 
        {
         log_not_found off;
         expires 30d;
         break;
        }
      location /status
      {
        access_log /usr/local/nginx/logs/status.log;
        auth_basic "NginxStatus";
        allow 10.0.209.0/24;
        deny all;
       }
        error_page  404              /404.html;
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }
0 0