【linux技術】nginx詳解

1 配置文件

    # worker進程數,通常設置成和cpu的數量相等
    worker_processes  auto;
    # 設置worker進程最大文件打開數;避免出現too many open files
    worker_rlimit_nofile 65535;
    # nginx的pid文件目錄
    pid   /var/run/nginx.pid;

    # events模塊:處理所有連接的設置
    events {
            # 多路複用IO(uname -a查看linux版本,linux2.6版本以上)
            use   epoll;
            # 每個worker進程同時打開的最大連接數
            worker_connections  1024;
                    }

    # http服務器,利用反向代理提供負載均衡
    http {
            #設定mime類型,類型由mime.type文件定義
            include       /etc/nginx/mime.types;
            default_type  application/octet-stream;
            # 設定日誌格式和access_log
            # 如果nginx作爲web服務器,和客戶端隔着反向代理層
            log_format combined '$remote_addr-$remote_user [$time_local]'
                                                '"$request"$status $body_bytes_sent'
                                                '"$http_referer" "$http_user_agent"'
            access_log    /var/log/nginx/access.log;

            # 配置多個虛擬主機需要增加此字段
            server_names_hash_bucket_size  512;
            # 隱藏nginx版本號:瀏覽器訪問時 http頭部沒有版本號
            server_tokens off;
            # 客戶端連接超時時間:超過時間關閉連接
            keepalive_timeout  65;
            # nginx作爲web服務器有用(一個高效的系統調用接口,輸出文件);反向代理服務器沒用
            sendfile   on;

            # 設置nginx報文大小,避免出現413 Request Entity Too Large.
            client_max_body_size 70m;
            # 作爲反向代理使用,此處禁用掉;反向代理收到服務端請求立馬返回給客戶端
            proxy_buffering off;

            # 開啓gzip壓縮:加載網頁數據採用gzip壓縮,大大提高傳輸速率
            gzip  on;
            # 處理壓縮的緩衝區大小;以4k爲單位,申請16倍的內存空間 建議保持默認即可
            gzip_buffers 4 16k;
            # gzip默認版本是http/1.1;默認http/1.0不支持gzip功能
            gzip_http_version 1.0;
            # 壓縮比:1-9的整數 數字越大 壓縮比越高 越耗費資源
            gzip_comp_level 1;
            # 壓縮類型
            gzip_types  text/htm text/plain application/x-javascript text/css  application/xml  text/javascript;
            # 是否添加vary頭部:校驗信息
            gzip_vary on;
            # nginx作爲反向代理使用,此處無條件壓縮所有數據;web服務器沒用
            gzip_proxied any;

            # nginx虛擬主機配置
            include /etc/nginx/conf.d/*.conf;

            # lua腳本相關知識

            # server字段:虛擬主機
            server {}

            }

2. 虛擬主機

    價值:
            單臺物理服務器可以設置虛擬主機,提供多個web服務

    配置: // server字段代表虛擬主機配置
            // 1.基於域名的虛擬主機
            server {
                    listen 80;
                    server_name  bbs1.young.com;
                    root  /wls/nginx/html;

                    location /bbs {    // location和root查找:root路徑 + location路徑
                            index  index.html;
                            }
                            }
             server {
                     listen 80;
                     server_name  bbs2.young.com;

                     location /bbs {
                             alias  /wls/nginx/html/bbs/;    // alias:1> 必須放在location裏面; 2> bbs/目錄必須增加 /
                             index  index.html;
                                    }
                        }

            // 2. 基於端口的虛擬主機 ip+端口實現
            server {
                    listen 8080;
                    server_name  bbs.young.com;
                    root  /wls/nginx/html;

                    location /bbs {    // location和root查找:root路徑 + location路徑
                            index  index.html;
                                    }
                        }
             server {
                     listen 8081;
                     server_name  bbs.young.com;

                     location /bbs {
                             alias  /wls/nginx/html/bbs/;    // alias:1> 必須放在location裏面; 2> bbs/目錄必須增加 /
                             index  index.html;
                            }
                        }
           // 3. 基於ip的虛擬主機  
            用的比較少

3. 反向代理

    基本概念:
            1. 反向代理
                針對服務端代理:客戶端實際訪問的是代理,並不知道後端真實服務器 // 不會暴漏後端服務器;可以做一些安全策略
            2. 正向代理
                針對客戶端代理:客戶端知道自己訪問真實服務器,但是自己沒有權限,可以設置代理代替自己去訪問
            3. 負載均衡
                單臺服務器的性能達到瓶頸,需要集羣提供更優的集羣服務;而負載均衡是可以將請求分發到後端服務器,提供更高的併發度和更好的性能。

     反向代理實現負載均衡
     // 此處訪問http://bbs.young.com --> http://21.68.9.24:8080 / http://21.68.9.24:8081處理
     http {
             upstream bbs_zone {  // upstream模塊 實現負載均衡
                     server  21.68.9.24:8080 weight=10;  // 基於端口的虛擬主機 設置權重
                     server  21.68.9.24:8081 weight=20;
                     }

             server {
                     listen 80;
                     server_name bbs.young.com;

                     location / {
                             proxy_pass  http://bbs_zone;  // proxy_pass模塊實現反向代理
                             }
                     }
                    }
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章