首頁  >  文章  >  後端開發  >  Nginx伺服器設定指令

Nginx伺服器設定指令

WBOY
WBOY原創
2016-07-30 13:30:091289瀏覽

Nginx伺服器設定指令:
Rewrite功能,代理功能

Rewrite功能

後端伺服器群組的設定指令

upstream指令

upstream指令是設定後端伺服器群組的主要指令

<code>upstream name {<span>...</span>} </code>

請求依照輪叫調度(Round-Robin,RR)策略順序選擇伺服器處理

server

server指令用於設定群組內的伺服器

<code>server address [<span>params</span>];</code>
  • address:伺服器位址,可以包含連接埠號碼或是以」unix:」為前綴的進程間通訊的Unix Domain Socket
  • params:為目前伺服器配置更多屬性。
    weight=number,群組內伺服器權重,權重高的優先處理請求(採用加權輪詢策略)
    max_fails=number,設定一個請求失敗的次數,當對群組內伺服器請求失敗次數超過該變數時,認為該伺服器無效(404除外)
    fail_timeout=time,設定嘗試請求某台群組內伺服器的時間,檢查伺服器是否有效
    backup,將伺服器標記為備用伺服器
    down,標記伺服器永久失效

ip_hash指令

ip_hash指令用於實現會話保持功能,將某個客戶端的多次請求定向到組內同一台伺服器上,保證客戶端與伺服器之間建立穩定的會話。
註:ip_hash指令不能與weight變數一起使用,在整個系統中,Nginx伺服器必須處於最前端的伺服器,而且客戶端位址必須為C類位址

keepalive指令

keepalive指令用於控制網路連線保持指令功能

<code>keepalive connections;</code>

設定伺服器的每個工作進程允許該伺服器群組保持的空閒網路連線數的上限值

least_conn指令

least_conn指令用於設定Nginx伺服器使用負載平衡策略為網路連線分配伺服器群組內的伺服器,將請求指派給目前網路連線最少的伺服器

Rewrite功能的設定指令

Rewrite功能的多種應用

代理功能

Nginx正向代理服務設定指令

指定DNS伺服器的IP位址

<code>resolver address <span>...</span> [valid=time];</code>

address,DNS伺服器的IP位址,預設連接埠35

    time,設定封包在網路中的有效時間
  • resolve_timeout指令
  • 逾時時間
<code>resolve_timeout <span>time</span>;</code>

proxy_pass指令

proxy_pass指令用來設定代理伺服器的協定與位址

<code>proxy_pass <span>URL</span>;</code>

Nginx反向代理服務設定指令

proxy_pass指令

proxy_Didment指令

proxy_pass指令

proxy_pass_Defxy_pass名稱、IP位址加上連接埠號形式等

<code>proxy_pass <span>URL</span>;</code>

proxy_hide_header指令

proxy_hide_header指令用於設定Nginx伺服器在傳送HTTP回應時,隱藏一些頭域資訊

<code>proxy<span>\_</span>hide_header field</code>

proxy_pass_erproheaderproheaderproheader被傳送

<code>proxy<span>\_</span>hide_header field</code>

proxy_pass_header指令

proxy_pass_header指令用來設定那些頭域資訊被傳送

<code>proxy<span>\_</span>hide_header field</code>

Nginx反向代理服務-負載負載無動權規則。的負載平衡

<code><span>...</span>
upstream backend {
    server <span>192.168</span><span>.1</span><span>.2</span>:<span>80</span>;
    server <span>192.168</span><span>.1</span><span>.3</span>:<span>80</span>;
    server <span>192.168</span><span>.1</span><span>.4</span>:<span>80</span>;
}
server {
    listen <span>80</span>;
    server_name www.mysite.name;
    index index.html index.htm;
    location / {
        proxy_pass http://backend;
        proxy_set_header Host $host;
        <span>...</span>
    }
    <span>...</span>
}</code>

特定資源的負載平衡

<code><span>...</span>
upstream backend {
    server <span>192.168</span><span>.1</span><span>.2</span>:<span>80</span> weight=<span>5</span>;
    server <span>192.168</span><span>.1</span><span>.3</span>:<span>80</span> weight=<span>2</span>;
    server <span>192.168</span><span>.1</span><span>.4</span>:<span>80</span>;
}
server {
    listen <span>80</span>;
    server_name www.mysite.name;
    index index.html index.htm;
    location / {
        proxy_pass http://backend;
        proxy_set_header Host $host;
        <span>...</span>
    }
    <span>...</span>
}</code>

對不同網域實現負載平衡

<code><span>...</span>
upstream videobackend {
    server <span>192.168</span><span>.1</span><span>.2</span>:<span>80</span>;
    server <span>192.168</span><span>.1</span><span>.3</span>:<span>80</span>;
    server <span>192.168</span><span>.1</span><span>.4</span>:<span>80</span>;
}
upstream filebackend {
    server <span>192.168</span><span>.1</span><span>.5</span>:<span>80</span>;
    server <span>192.168</span><span>.1</span><span>.6</span>:<span>80</span>;
    server <span>192.168</span><span>.1</span><span>.7</span>:<span>80</span>;
}
server {
    listen <span>80</span>;
    server_name www.mysite.name;
    index index.html index.htm;
    location /video/ {
        proxy_pass http://videobackend;
        proxy_set_header Host $host;
        <span>...</span>
    }
    location /file/ {
        proxy_pass http://filebackend;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        <span>...</span>
    }
    <span>...</span>
}</code>

帶有URL重寫的負載平衡

<code><span>...</span>
upstream bbsbackend{
    server <span>192.168</span><span>.1</span><span>.2</span>:<span>80</span> weight=<span>2</span>;
    server <span>192.168</span><span>.1</span><span>.3</span>:<span>80</span> weight=<span>2</span>;
    server <span>192.168</span><span>.1</span><span>.4</span>:<span>80</span>;
}
upstream homebackend {
    server <span>192.168</span><span>.1</span><span>.4</span>:<span>80</span>;
    server <span>192.168</span><span>.1</span><span>.5</span>:<span>80</span>;
    server <span>192.168</span><span>.1</span><span>.6</span>:<span>80</span>;
}
server {
    listen <span>80</span>;
    server_name home.mysite.name;
    index index.html index.htm;
    location / {
        proxy_pass http://homebackend;
        proxy_set_header Host $host;
        <span>...</span>
    }
    <span>...</span>
}

server {
    listen <span>81</span>;
    server_name bbs.mysite.name;
    index index.html index.htm;
    location / {
        proxy_pass http://bbsbackend;
        proxy_set_header Host $host;
        <span>...</span>
    }
    <span>...</span>
}</code>

以上就介紹了Nginx伺服器設定指令,包括了方面的內容,希望對PHP教學有興趣的朋友有幫助。

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn