Nginx負載均衡配置文件詳解之一(初級詳解)

由於本人也是剛學的nginx,以下對自己的一點體會做個說明,紅字部分爲個人說明。
#user  nobody;
worker_processes  1;

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;


events {
    worker_connections  1024;  # worker_connections*worker_process=max_clients_connections  在作爲反向代理時,max_clients變爲:max_clients = worker_processes * worker_connections/4。
}


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 tomcat{
        server 127.0.0.1:8080;
    }
    upstream apache{
        server 127.0.0.1:8081;
    }
    server {
        listen       80;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;
    
       # location / {
       #     root   html;
       #     index  index.html index.htm;

       # }

#location 爲進入nginx請求後首先執行的步驟,其中 proxy_pass http://tomcat/* 【tomcat與上面的某個upstream對應,“tomcat/abc”後面的字符串abc代表某個we

#容器下的路徑,如本例中,location /  代表請求地址,如http://127.0.0.1/,如果是location /abc/ 代表 http://127.0.0.1/abc/  。 然後根據location{ }中proxy_pass的域##名部分如 http://tomcat/kayouba的域名部分爲tomcat,定位upstream, 本例中定位到:

  #  upstream tomcat{
 #       server 127.0.0.1:8080;
 #   }

然後跳轉到127.0.0.1:8080所指向的服務器,並定位到服務器下面的myweb應用

#】

    location /{
        root   kayoubaweb/;
            index  index.html index.htm index.jsp;

        proxy_pass http://tomcat/myweb/;
        
        client_max_body_size          20m;
            client_body_buffer_size       128k;

            proxy_connect_timeout         120;
            proxy_send_timeout            120;
            proxy_read_timeout            120;

            proxy_buffer_size             32k;
            proxy_buffers                 4 128k;
            proxy_busy_buffers_size       256k;
            proxy_temp_file_write_size    256k;
    }
    location /apache {
        root   html;
            index  index.html index.htm;
        autoindex              on;
        proxy_pass http://apache/home/;
        
        client_max_body_size          20m;
            client_body_buffer_size       128k;

            proxy_connect_timeout         120;
            proxy_send_timeout            120;
            proxy_read_timeout            120;

            proxy_buffer_size             32k;
            proxy_buffers                 4 128k;
            proxy_busy_buffers_size       256k;
            proxy_temp_file_write_size    256k;
    }
        error_page  404 =200            /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;
        #}
    }


    # 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;
    #    server_name  localhost;

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

    #    ssl_session_timeout  5m;

    #    ssl_protocols  SSLv2 SSLv3 TLSv1;
    #    ssl_ciphers  HIGH:!aNULL:!MD5;
    #    ssl_prefer_server_ciphers   on;

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

}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章