Linux Nginx 配置(二)

这次配置实现的功能是,两台服务器,每台服务器上各一个Tomcat,使用ip_hash实现负载均衡、解决session问题,静态资源的配置

目前公司域名还没有下来,之后配置域名访问

首先,进入nginx目录,打开配置文件

cd /usr/local/nginx
cat con/nginx_conf

然后按照如下进行配置

#user  nobody;
worker_processes  3;    #这里有的说跟CPU核数一样,有的说比CPU核数少1,我是按照少1配的

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;
error_log /home/XXXX/logs/nginx/error.log crit;    #配的日志地址,到目前为止,这个日志还是空的

#pid        logs/nginx.pid;
pid /usr/local/nginx/logs/nginx.pid;    #nginx的进程号

events {
    worker_connections  1024;
}


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;

    #负载均衡
    upstream tomcats {
        ip_hash;    #使用ip地址的前三段来进行hash计算,解决了session的问题
        server 172.16.14.40:8180;    #第一台服务器,在这里,可以只有一台服务器,配两个Tomcat,端口不一样
        server 172.16.14.39:8180;    #第二台服务器
    }

    server {
        listen       80;    #监听80端口,这样访问的时候不用输入端口号
        server_name  47.96.237.216;    #后期这里改成域名

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        #location / {
        #    root   html;
        #    index  index.html index.htm;
        #}

        #error_page  404              /404.html;

        # redirect server error pages to the static page /50x.html
        #
        #error_page   500 502 503 504  /50x.html;
        #location = /50x.html {
        #    root   html;
        #}

        # proxy the PHP scripts to Apache listening on 127.0.0.1:80
        #
        #location ~ \.php$ {
        #    proxy_pass   http://127.0.0.1;
        #}

        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        #location ~ \.php$ {
        #    root           html;
        #    fastcgi_pass   127.0.0.1:9000;
        #    fastcgi_index  index.php;
        #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
        #    include        fastcgi_params;
        #}

        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        #location ~ /\.ht {
        #    deny  all;
        #}

        # 8180-lbqb
        #用于跳转到lbqb
        location /lbqb/ {
            proxy_set_header X-real-ip $remote_addr;    #为了在程序中可以获取到访问者真实的ip,不然获取的是nginx代理的ip
            proxy_pass http://tomcats/lbqb/;
            client_max_body_size 6m;    #tomcat默认文件大小限制是10m,而nginx是1m,在这里重新配置
        }

        location / {
            root html/XXX/XXX;    #静态资源文件目录
            index index.html;
            try_files $uri $uri/ /index.html;
        }
    }


    # another virtual host using mix of IP-, name-, and port-based configuration
    #
    #server {
    #    listen       8000;
    #    listen       somename:8080;
    #    server_name  somename  alias  another.alias;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}


    # HTTPS server
    #
    #server {
    #    listen       443 ssl;
    #    server_name  localhost;

    #    ssl_certificate      cert.pem;
    #    ssl_certificate_key  cert.key;

    #    ssl_session_cache    shared:SSL:1m;
    #    ssl_session_timeout  5m;

    #    ssl_ciphers  HIGH:!aNULL:!MD5;
    #    ssl_prefer_server_ciphers  on;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}

}

你可能感兴趣的:(Linux,Nginx)