Nginx Wiki(已過時最新:http://nginxorg/en/docs/)

WBOY
發布: 2016-07-30 13:30:47
原創
1717 人瀏覽過
概要此模組可以將請求傳送到另一台伺服器。 範例:

<span>location</span> / <span>{</span>
  proxy_pass        <span>http</span>://localhost:<span>8000</span><span>;</span>
  proxy_set_header  X-Real-IP  <span>$remote_addr</span><span>;</span><span>}</span>
登入後複製
請注意,當使用 HTTP 代理模組時(甚至使用 FastCGI 時),整個客戶端請求將在傳遞到後端代理伺服器之前在 nginx 中進行緩衝。因此,上傳進度表即使工作也無法正常運作 透過測量後端伺服器接收到的數據。 指令proxy_bind語法: proxy_bind位址 http,伺服器, location版本: ≥ 0.8.22範例:
proxy_bind  192.168.1.1<span>;</span>
登入後複製
該指令在呼叫connect() 之前將每個上游套接字綁定到本地位址。如果主機有多個介面/別名,並且您想要從特定介面/位址傳遞傳出連接,這可能很有用。

proxy_buffer_size語法: proxy_buffer_size 4k|8k 上下文: http伺服器位置 參考:
proxy_buffer_size

此指令設定緩衝區大小,將從代理伺服器取得的回應的第一部分讀取到其中。 通常,小響應標頭位於響應的這一部分。 預設情況下,緩衝區大小等於指令proxy_buffers中一個緩衝區的大小;但是,可以設定它 proxy_buffering 語法: proxy_buffering  上下文: http 伺服器位置 參考錯誤:
proxy_buffering

此指令啟動代理伺服器的回應緩衝。 如果啟動緩衝,則 nginx 會盡快從代理伺服器讀取答案,並將其保存在指令proxy_buffer_size和proxy_buffers 配置的緩衝區中。 如果回應無法放入內存,則部分回應將寫入磁碟。 如果關閉緩衝,則回應在收到後立即同步傳輸到客戶端。 nginx 不會嘗試從代理伺服器讀取整個答案,nginx 可以從伺服器接受的最大資料大小是 透過指令proxy_buffer_size設定。 另請注意,如果 proxy_buffering 設定為 off,快取上游代理回應將無法運作。 對於基於長輪詢的 Comet 應用程序,將 proxy_buffering 設為 off 很重要,否則非同步回應已緩衝,Comet 不起作用。 可以透過在代理回應中設定 X-Accel-Buffering 標頭來基於每個請求設定緩衝。 proxy_buffers 語法: proxy_buffers 8 4k|8k 上下文: http 伺服器位置 參考:
proxy_buffers
。預設情況下,一個緩衝區的大小等於一頁的大小。根據平台的不同,該值可以是 4K 或 8K。 proxy_busy_buffers_size
語法:proxy_busy_buffers_sizeizesize 8k|16k 上下文: http伺服器位置 參考: proxy_busy_buffers_size

proxy_cache 文法: 預設: off 上下文: http伺服器位置 參考錯誤:

proxy_cache proxy_cache proxy_cache
proxy_cacheproxy_cacheproxy_cache
  • proxy_cache
  • proxy_cacheproxy_cache pro同一區域可以在多個地方使用。 自版本 0.7.48 以來,快取支援後端的「Expires」、「Cache-Control: no-cache」和「Cache-Control: max-age=XXX」標頭。從 7.66 版本開始,「私有」和「無儲存」也受到尊重。 nginx 在快取時不處理「Vary」標頭。為了確保 私有專案不會無意中透過快取提供給所有用戶,後端可以設定“no-cache”或“max-age=0”,或 proxy_cache_key 必須 包括使用者特定的數據,例如 $cookie_xxx。但是,使用 cookie 值作為 proxy_cache_key 的一部分可能會抵消公共專案快取的優勢,因此使用不同的 proxy_cache_key 值來區分位置 可能需要分隔私有和公有項目。 快取取決於代理緩衝區,如果 proxy_buffers 設定為關閉,則該快取將無法運作。 以下回應標頭將回應標記為不可緩存,除非它們被忽略: 設定-Cookie Cache-Control 包含「no-cache」、「no-store」、「private」或具有非數字或0 值的「max-age」 過期時間為過去 X-Accel-Expires: 0 proxy_cache_bypass
    ache
    _p string ... 預設:  🎜🎜🎜上下文:🎜🎜🎜httpache🎜 🎜🎜🎜🎜
    The directive specifies the conditions under which the answer will not be taken from the cache. If at least one of a string variable is not empty and not equal to "0", the answer is not taken from the cache:

     proxy_cache_bypass <span>$cookie_nocache</span><span>$arg_nocache</span><span>$arg_comment</span><span>;</span>
     proxy_cache_bypass <span>$http_pragma</span><span>$http_authorization</span><span>;</span>
    登入後複製
    Note that the response from the back-end is still eligible for caching. Thus one way of refreshing an item in the cache is sending a request with a header you pick yourself, e.g. "My-Secret-Header: 1", then having a proxy_cache_bypass line like:

    proxy_cache_bypass <span>$http_my_secret_header</span><span>;</span>
    登入後複製
    Can be used in conjunction with the directive proxy_no_cache.proxy_cache_key Syntax: proxy_cache_key string Default: $scheme$proxy_host$request_uri Context: http
    server
    location Reference: proxy_cache_key
    The directive specifies what information is included in the key for caching, for example

    proxy_cache_key <span>"$host$request_uri$cookie_user"</span><span>;</span>
    登入後複製
    Note that by default, the hostname of the server is not included in the cache key. If you are using subdomains for different locations on your website, you need to include it, e.g. by changing the cache key to something like

    proxy_cache_key <span>"$scheme$host$request_uri"</span><span>;</span>
    登入後複製
    proxy_cache_lock Syntax: proxy_cache_lock on | off Default: off Context: http
    server
    location Appeared in: 1.1.12 Reference: proxy_cache_lock When enabled, only one request at a time will be allowed to populate a new cache element identified according to the proxy_cache_key directive by passing a request to a proxied server. Other requests of the same cache element will either wait for a response to appear in the cache or the cache lock for this element to be released, up to the time set by the proxy_cache_lock_timeout directive. Similar effect for updating cache entry (this directive works only for inserting new cache element) can be archieved by using proxy_cache_use_stale updating directive.proxy_cache_lock_timeout Syntax: proxy_cache_lock_timeout time Default: 5s Context: http
    server
    location Appeared in: 1.1.12 Reference: proxy_cache_lock_timeout
    proxy_cache_methodssyntax: proxy_cache_methods [GET HEAD POST];default: proxy_cache_methods GET HEAD;context: http, server, locationGET/HEAD is syntax sugar, i.e. you can not disable GET/HEAD even if you set just

    proxy_cache_methods POST<span>;</span>
    登入後複製
    proxy_cache_min_uses Syntax: proxy_cache_min_uses number Default: 1 Context: http
    server
    location Reference: proxy_cache_min_uses
    查詢數量,之後回覆將會被快取。 proxy_cache_path 語法: proxy_cache_path keys_zone = 名稱 : 大小 [不活動 = 時間 ] [max_size = size ] [loader_files = number ] [loader_sleep = time ] [loader_threshold = time ] http 參考: proxy_cache_path
    This directive sets the cache path and other cache parameters. Cached data is stored in files. An MD5 hash of the proxied URL is used as the key for the cache entry, and is also used as the filename in the cache path for the response contents and metadata. The levels parameter sets the number of subdirectory levels in cache. For example:

    proxy_cache_path  /data/nginx/cache/one  levels<span>=</span><span>1</span>:<span>2</span>   keys_zone<span>=</span>one:10m<span>;</span>
    登入後複製
    In this cache, file names will be like the following:

    <span>/</span>data<span>/</span>nginx<span>/</span>cache<span>/</span>c<span>/</span><span>29</span><span>/</span>b7f54b2df7773722d382f4809d65029c
    登入後複製
    You may use any combination of 1 and 2 in the level formats: X, X:X, or X:X:X e.g.: "2", "2:2", "1:1:2". There can be at most 3 levels.All active keys and metadata is stored in shared memory. Zone name and the size of the zone is defined via the keys_zone parameter.Note that each defined zone must have a unique path. For example:

    proxy_cache_path  /data/nginx/cache/one    levels<span>=</span><span>1</span>      keys_zone<span>=</span>one:10m<span>;</span>
    proxy_cache_path  /data/nginx/cache/two    levels<span>=</span><span>2</span>:<span>2</span>    keys_zone<span>=</span>two:100m<span>;</span>
    proxy_cache_path  /data/nginx/cache/three  levels<span>=</span><span>1</span>:<span>1</span>:<span>2</span>  keys_zone<span>=</span>three:1000m<span>;</span>
    登入後複製
    If cached data is not requested for time defined by the inactive parameter, than that data is removed from the cache. The inactive parameter defaults to 10 minutes (10m).A special process, called "cache manager", is created to control the on-disk cache. It is responsible for removing inactive items and enforcing the size of the cache, as defined by the parameter max_size. When the total size of the cache exceeds the maximum size set by max_size, the least recently used data in the cache is deleted to make room for a new cache entry (a LRU replacement policy).Zone size should be set proportional to number of pages to cache. The size of the metadata for one page (file) depends on the OS; currently it is 64 bytes for FreeBSD/i386, and 128 bytes for FreeBSD/amd64.The directories specified by proxy_cache_path and proxy_temp_path should be located on the same filesystem.proxy_cache_use_stale 語法: proxy_cache_use_stale 錯誤 | http_502 | http_503 | http_504 | http_404 | off ...預設: off proxy_cache_use_stale

    該指令告訴Nginx 何時從代理快取提供過時的項目。此指令的參數類似於 proxy_next_upstream 新增“updating”。 為了防止快取踩踏(當多個執行緒踩踏嘗試同時更新快取時),您可以指定「updating」參數。這將導致一個線程更新緩存,並且在更新過程中所有其他線程都將提供服務 快取中內容的過時版本。 proxy_cache_valid 語法:
    proxy_cache_valid 預設: 上下文: http伺服器位置參考: proxy_cache_valid
    This directive sets the time for caching different replies. Example:

    proxy_cache_valid  <span>200</span><span>302</span>  10m<span>;</span>
    proxy_cache_valid  <span>404</span>      1m<span>;</span>
    登入後複製
    sets 10 minutes cache time for replies with code 200 and 302, and 1 minute for 404s.If only time is specified:

    proxy_cache_valid 5m<span>;</span>
    登入後複製
    then only replies with codes 200, 301 and 302 will be cached.Also it is possible to cache any replies with parameter "any":

    proxy_cache_valid  <span>200</span><span>302</span> 10m<span>;</span>
    proxy_cache_valid  <span>301</span> 1h<span>;</span>
    proxy_cache_valid  any 1m<span>;</span>
    登入後複製
    Upstream cache-related directives have priority over proxy_cache_valid value, in particular the order is (from Igor):
  • X-Accel-Expires
  • Expires/Cache-Control
  • proxy_cache_valid
  • The order in which your backend return HTTP headers change cache behaviour. Read this post for details.You may ignore the headers using

    proxy_ignore_headers X-Accel-Expires Expires Cache-Control<span>;</span>
    登入後複製
    Concerning If-Modified / Last-Modified since behaviour, please remember that by default nginx sends 304 only if L-M == I-M-S. Controlled by directive if_modified_since [off|exact|before]Note: you must set this option for any persistent caching to occur.proxy_connect_timeout Syntax: proxy_connect_timeout time Default: 60s Context: http
    server
    location Reference: proxy_connect_timeout
    該指令為與上游伺服器的連線分配逾時。需要記住的是,這個超時時間不能超過 75 秒。 這不是伺服器回傳頁面的時間,而是 proxy_read_timeout 語句。如果你的 上游伺服器已啟動,但掛起(例如,它沒有足夠的線程來處理您的請求,因此它將您放入連接池中以供稍後處理),那麼此語句將無濟於事,因為與伺服器的連接已經建立。 proxy_cookie_domain 語法: proxy_cookie_domainookie
    預設:
    關閉 上下文: http伺服器位置

    http伺服器 1.1.15 proxy_cookie_path 語法:
    proxy_cookie_path off proxy_cookie_path path proxy_cookie_path 
    上下文:http
    伺服器位置 出現於: 1.1.15 參考: proxy_cookie_path

    proxy_headers_hash_bucket_size語法: proxy_heads_pash_buck語法: proxy_i_pash_buck ers_hash _bucket_size 64;上下文: http,伺服器,位置,if這個指令設定標頭哈希表的桶大小。 這決定了標頭名稱的限制。如果您使用的標頭名稱長度超過 64 個字符,請增加此值。
    proxy_headers_hash_max_size語法: proxy_headers_hash_max_size size;_; 上下文: http,伺服器, location, if該指令設定headers哈希表的最大大小。 不得小於您後端設定的標頭數量。 proxy_hide_header
    語法: proxy_hide_header   上下文: http伺服器位置 參考: proxy_hide_header


    nginx does not transfer the "Date", "Server", "X-Pad" and "X-Accel-..." header lines from the proxied server response. The proxy_hide_header directive allows to hide some additional header lines. But if on the contrary the header lines must be passed, then the proxy_pass_header should be used. For example if you want to hide the MS-OfficeWebserver and the AspNet-Version:

    <span>location</span> / <span>{</span>
      proxy_hide_header X-AspNet-Version<span>;</span>
      proxy_hide_header MicrosoftOfficeWebServer<span>;</span><span>}</span>
    登入後複製
    This directive can also be very helpful when using X-Accel-Redirect. For example, you may have one set of backend servers which return the headers for a file download, which includes X-Accel-Redirect to the actual file, as well as the correct Content-Type. However, the Redirect URL points to a files erver which hosts the actual file you wish to serve, and that server sends its own Content-Type header, which might be incorrect, and overrides the header sent by the original backend servers. You can avoid this by adding the proxy_hide_header directive to the fileserver. Example:

    <span>location</span> / <span>{</span>
      proxy_pass <span>http</span>://backend_servers<span>;</span><span>}</span>
     
    <span>location</span> /files/ <span>{</span>
      proxy_pass <span>http</span>://fileserver<span>;</span>
      proxy_hide_header Content-Type<span>;</span><span>}</span>
    登入後複製
    proxy_http_version Syntax: proxy_http_version 1.0 | 1.1 Default: 1.0 Context: http
    server
    location Appeared in: 1.1.4 Reference: proxy_http_version
    proxy_ignore_client_abort Syntax: proxy_ignore_client_abort on | off Default: off Context: http
    server
    location Reference: proxy_ignore_client_abort
    防止在客戶端本身中止請求時中止對代理的請求。 proxy_ignore_headers 語法: proxy_ignore_headers proxy_ignore_headers   上下文: http伺服器位置

    參考: proxy_ignore_headers 禁止處理代理伺服器中的標行。 它可以指定字串為「X-Accel-Redirect」、「X-Accel-Expires」、「Expires」、「Cache-Control」或「Set-Cookie」。預設情況下, nginx 不會快取帶有 Set-Cookie 的請求。
    proxy_intercept_errors語法: proxy_intercept_ 預設: 關閉 上下文: http 伺服器位置 參考: proxy_intercept_errors

    預設情況下,所有回應都將從代理伺服器原樣發送。 如果您將其設定為on,那麼 nginx 將攔截狀態代碼由 error_page 指令明確處理。 狀態代碼與 error_page 指令不符的回應將從代理伺服器原樣發送。 proxy_max_temp_file_size
    語法:proxy_max_temp_sizesize 1024m 上下文:http伺服器位置 參考: proxy_max_temp _檔案大小
    The maximum size of a temporary file when the content is larger than the proxy buffer. If file is larger than this size, it will be served synchronously from upstream server rather than buffered to disk.If proxy_max_temp_file_size is equal to zero, temporary files usage will be disabled.proxy_methodsyntax: proxy_method [method];default: Nonecontext: http, server, locationAllows you to override the HTTP method of the request to be passed to the backend server. If you specify POST for example, all requests forwarded to the backend server will be POST requests.Example:

    proxy_method POST<span>;</span>
    登入後複製
    proxy_next_upstream Syntax: proxy_next_upstream error | timeout | invalid_header | http_500 | http_502 | http_503 | http_504 | http_404 | off ... Default: error timeout Context: http
    server
    location Reference: proxy_next_upstream
    Directive determines in what cases the request will be transmitted to the next server:
  • error — an error has occurred while connecting to the server, sending a request to it, or reading its response;
  • timeout — occurred timeout during the connection with the server, transfer the request or while reading response from the server;
  • invalid_header — server returned a empty or incorrect answer;
  • http_500 — server returned answer with code 500
  • http_502 — server returned answer with code 502
  • http_503 — server returned answer with code 503
  • http_504 — server returned answer with code 504
  • http_404 — server returned answer with code 404
  • off — it forbids the request transfer to the next server
  • Transferring the request to the next server is only possible when nothing has been transferred to the client -- that is, if an error or timeout arises in the middle of the transfer of the request, then it is not possible to retry the current request on a different server.proxy_no_cache Syntax: proxy_no_cache string ... Default:   Context: http
    server
    location Reference: proxy_no_cache
    Specifies in what cases a response will not be cached, e.g.

    proxy_no_cache <span>$cookie_nocache</span><span>$arg_nocache</span><span>$arg_comment</span><span>;</span>
    proxy_no_cache <span>$http_pragma</span><span>$http_authorization</span><span>;</span>
    登入後複製
    The response is marked uncacheable if any of the arguments expand to anything other than "0" or the empty string. For instance, in the above example, the response will never be cached if the cookie "nocache" is set in the request.proxy_pass Syntax: proxy_pass URL Default:   Context: location
    if in location
    limit_except Reference: proxy_pass
    This directive sets the address of the proxied server and the URI to which location will be mapped. Address may be given as hostname or address and port, for example,

    proxy_pass <span>http</span>://localhost:<span>8000</span>/uri/<span>;</span>
    登入後複製
    or as unix socket path:

    proxy_pass <span>http</span>://unix:/path/to/backend.socket:/uri/<span>;</span>
    登入後複製
    path is given after the word unix between two colons.By default, the Host header from the request is not forwarded, but is set based on the proxy_pass statement. To forward the requested Host header, it is necessary to use:

    proxy_set_header Host <span>$host</span><span>;</span>
    登入後複製
    While passing request nginx replaces URI part which corresponds to location with one indicated in proxy_pass direct
    相關標籤:
    來源:php.cn
    上一篇:php可變參數實現 下一篇:php匿名函數的用法
    本網站聲明
    本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
    作者最新文章
    最新問題
    相關專題
    更多>
    熱門推薦
    熱門教學
    更多>
    最新下載
    更多>
    網站特效
    網站源碼
    網站素材
    前端模板