Nginx 常用技術

目錄

       1 nginx介紹

        2 nginx 常用命令(linux)

        3 nginx 配置文件說明

        4 nginx做http服務器(反向代理)

        5 nginx 做圖片服務器

        7 負載均衡

        8 動靜分離

        9 高併發

總結


1 nginx介紹

nginx是一個web服務器,通常用來實現服務器的反向代理。

常見請求與響應有:正常請求、正向代理、反向代理。

1.1 正常請求

這是開發中比較常見的方式,由客戶端直接請求服務器,然後服務器直接對客戶端響應數據。如下圖所示。

Alt text

1.2 正向代理

客戶端不能直接請求原始服務器,需要先將請求與請求地址發送給代理服務器,然後由代理服務去將請求轉發到原始服務器。原始服務器將結果響應給代理服務器,然後由代理服務器將得到的響應結果返回給客戶端。如下圖所示。

Alt text

在此過程中,需要在客戶端配置代理服務器的地址(hosts文件)。正向代理是在客戶端使用的,是對客戶端的代理,由客戶端知道並去主動代理。(如翻牆等)

正向代理的作用:

1.用於訪問訪問不了的資源(如google翻牆;
2.可以做代理服務器緩存,加上資源訪問;
3.可以對客戶端上網進行認證授權(如校園網;
4.上網行爲的管理,對外隱藏用戶信息(原始服務器並不知道訪問用戶是誰。

1.3 反向代理

客戶端將請求發送到服務器(客戶端認爲是原始服務器,實際是反向代理服務器),反向代理服務器通過一定的策略將請求轉發到服務器集羣中的服務器上。然後由集羣服務器響應結果。反向代理服務去將結果返回給客戶端。

在此過程中。客戶端是不知道有沒有代理服務器。客戶端會認爲請求的就是原始服務器。如下圖所示。

Alt text

反向代理的作用:

1.負載均衡,提高請求處理與響應速度(集羣);
2.保證內網安全,隱藏服務器信息,防止web攻擊。

2 nginx 常用命令(linux)

默認端口80,配置文件nginx.conf,成功日誌access.log,失敗日誌error.log。

2.1 啓動nginx

sudo nginx 

或者
sudo nginx -c 配置文件路徑(nginx.conf)

2.2 重啓nginx

sudo nginx -s reload

2.3 停止nginx

sudo nginx -s stop

2.4 偵聽nginx端口

ps aux | grep nginx

或者
netstat -ntpl | grep 80

sudo netstat -antup|grep PID號

2.5 查看nginx版本

sudo nginx -v

2.6 測試nginx的配置文件是否有格式錯誤

sudo nginx -t

以下測試都是在windows系統測試。

3 nginx 配置文件說明

nginx.conf說明。

#nginx用戶
#user  nobody;

#工作進程數,由cpu核心總數決定最大值
worker_processes  1;

#錯誤日誌位置
#error_log  logs/error.log;

#PID文件位置(pid爲進程id號)
#pid        logs/nginx.pid;

#工作模式
events {
    #每個進程的最大連接數,默認1024;(如每個進程連接數1000,有兩個進程數。則可以提供2000個連接)
    worker_connections  1024;
}

#http相關與虛擬主機配置
http {
    #支持的媒體類型,類型在文件 mime.types中定義
    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函數輸出日誌
    sendfile        on;
    #tcp_nopush     on;

    #連接超時65s
    keepalive_timeout  65;

    #開啓gzip壓縮
    #gzip  on;

    upstream server_test {
        #加ip_hash後當前用戶的請求只能去同一個服務。
        #ip_hash;
        server 10.0.0.11:9090 down;#down 表示單前的server暫時不參與負載.
        #weight權重,max_fails :允許請求失敗的次數默認爲1.當超過最大次數時,返回proxy_next_upstream 模塊定義的錯誤.fail_timeout : max_fails次失敗後,暫停的時間。
        server 127.0.0.1:1111 weight=4 max_fails=3 fail_timeout=0s;
        server 127.0.0.1:8089 weight=1 max_fails=3 fail_timeout=0s;
        server 127.0.0.1:8088 backup;#backup: 其它所有的非backup機器down或者忙的時候,請求backup機器。所以這臺機器壓力會最輕。
    }

   server {
        listen       80;
        server_name  www.vue.com;
        charset utf-8;

        #access_log  logs/host.access.log  main;

        #1前端代理。可以是地址(項目地址G:/git/antd-demo/dist,dist 爲Vue項目build後),也可以使用tomcat
        location / {
            root    G:/git/antd-demo/dist;
            index   index.html;
        }

       #2圖片服務器,圖片地址G:/web/images/下
        location /images/ {  
            root G:/web;
            #開啓圖片瀏覽功能
            autoindex on;
        }

        #3後臺代理,前端訪問目錄名稱。前端訪問接口前加www.vue.com/test,如http://www.vue.com/test/user/login?'+new Date().getTime()
        location ^~ /test/ {
            #server_test對應upstream server_test
            proxy_pass   http://server_test/; 

            #連接時間超時時間設置
            #proxy_connect_timeout       1;
            #proxy_read_timeout          1;
            #proxy_send_timeout          1;
        }

        #4靜態資源F:/static下
        location  ~ .*\.(jpg|jpeg|gif|png|ico|css|js|pdf|txt)$ {  
            root G:/web/static;
        }

        error_page  404              /404.html;
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }
}

4 nginx做http服務器(反向代理)

每臺物理服務器劃分爲多個虛擬服務器,每個虛擬主機對應一個web站點。其實就是一臺服務器搭建了多個網站。

虛擬主機的地址區分方式:

1.ip區分(如一臺服務器有多個網卡,或者用多臺服務器)

2.端口區分(一臺服務器,ip相同,使用端口區分。後臺開發比較常用)

3.域名區分(實際網站中常用,需要花錢買域名。在該處使用將域名加到hosts文件的方式,只能用於本地測試)

主要配置

server {
    listen       80;
    server_name  www.vue.com;
    charset utf-8;

    #access_log  logs/host.access.log  main;

    #前端代理。可以是地址(項目地址G:/git/antd-demo/dist,dist 爲Vue項目build後),也可以使用tomcat
    location / {
        root    G:/git/antd-demo/dist;
        index   index.html;
    }

    error_page  404              /404.html;
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   html;
    }
}

在hosts中加入www.vue.com的DNS;

訪問www.vue.com,將轉向G:/git/antd-demo/dist/index.html;(以上dist是一個vue項目npm run build後得到)

5 nginx 做圖片服務器

(linux下需要ftp或者sftp進行上傳.)

server {
        #偵聽端口,默認80
        listen       80;
        #域名,實際中需要申請。該處測試的時候可以在在hosts中加 (ip localhost)
        server_name  www.vue.com;
        #編碼格式
        charset utf-8;

        #虛擬主機日誌
        #access_log  logs/host.access.log  main;

        #圖片服務器,圖片地址G:/web/images/下
        location /images/ {  
            root G:/web;
            #開啓圖片瀏覽功能
            autoindex on;
        }

        error_page  404   /404.html;
        location = /50x.html {
            root  html;
        }
    }
}

訪問http://www.vue.com/images/favicon.ico ( G:/web/images/favicon.ico)

7 負載均衡

下面展示當前端請求www.vue.com/test/.....相關接口時,轉發到127.0.0.1:8088或者127.0.0.1:8089下。

如有後臺接口http://127.0.0.1:8089/user/login;前端訪問寫爲 http://www.vue.com/test/user/login?'+new Date().getTime()

#後臺請求ip配置
upstream server_test {
        #加ip_hash後當前用戶的請求只能去同一個服務。
        #ip_hash;
        server 10.0.0.11:9090 down;#down 表示單前的server暫時不參與負載.
        #weight權重,max_fails :允許請求失敗的次數默認爲1.當超過最大次數時,返回proxy_next_upstream 模塊定義的錯誤.fail_timeout : max_fails次失敗後,暫停的時間。
        server 127.0.0.1:1111 weight=4 max_fails=3 fail_timeout=0s;
        server 127.0.0.1:8089 weight=1 max_fails=3 fail_timeout=0s;
        server 127.0.0.1:8088 backup;#backup: 其它所有的非backup機器down或者忙的時候,請求backup機器。所以這臺機器壓力會最輕。
}

server {
    #偵聽端口,默認80
    listen       80;
    #域名,實際中需要申請。該處測試的時候可以在在hosts中加 (ip localhost)
    server_name  www.vue.com;
    #編碼格式
    charset utf-8;

    #虛擬主機日誌
    #access_log  logs/host.access.log  main;

    #前臺代理,訪問http://localhost時進入前端目錄入口D:\Test\vue\vue1\dist\index.html
    location / {
        root     D:\Test\vue\vue1\dist;
        index    index.html;
    }

    #後臺代理,前端訪問目錄名稱。前端訪問接口前加www.vue.com/test,如http://www.vue.com/test/user/login?'+new Date().getTime()
    location ^~ /test/ {
        #server_test對應upstream server_test
        proxy_pass   http://server_test/; 

        #連接時間超時時間設置
        #proxy_connect_timeout       1;
        #proxy_read_timeout          1;
        #proxy_send_timeout          1;
    }

    #400錯誤頁面
    error_page  404   /404.html;
    #500錯誤頁面
    location = /50x.html {
        root  html;
    }
}

8 動靜分離

讓動態資源去後臺服務器請求,靜態資源去nginx請求。因爲nginx處理靜態資源速度快。

需要把靜態資源單獨拷貝處理,在nginx中配置路徑。

server {
        #偵聽端口,默認80
        listen       80;
        #域名,實際中需要申請。該處測試的時候可以在在hosts中加 (ip localhost)
        server_name  www.vue.com;
        #編碼格式
        charset utf-8;

        #虛擬主機日誌
        #access_log  logs/host.access.log  main;

        #靜態資源F:/static下
        location  ~ .*\.(jpg|jpeg|gif|png|ico|css|js|pdf|txt)$ {  
            root G:/web/static;
        }

        error_page  404   /404.html;
        location = /50x.html {
            root  html;
        }
    }
}

9 高併發

1.做負載均衡:集羣;

2.資源動靜分離(靜態資源由nginx提供,動態資源由tomcat提供:使用nginx或者DNS;

3.緩存:以空間換時間,提高效率;

4.限流:流量控制(隊列實現);

5.降級:在併發量特別高時,可以暫時關掉非核心服務。(如日誌等只保留報錯,警告日誌暫時關掉等)。

總結

nginx主要用來做反向代理、負載均衡、資源動靜分離以及圖片服務器。

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