각종 팁&정보를 나누는 곳입니다.
누군가에게 도움이 된다 싶으시면 언제라도 포스팅 :)
글 등록하기 | 내글 관리하기 | 연재글 | 보관함
NGINX REVERSE PROXY
첨부파일 https://imweb.eond.com/webtip/393454

This article describes the basic configuration of a proxy server. You will learn how to pass a request from NGINX to proxied servers over different protocols, modify client request headers that are sent to the proxied server, and configure buffering of responses coming from the proxied servers.

Table of Contents

Introduction

Proxying is typically used to distribute the load among several servers, seamlessly show content from different websites, or pass requests for processing to application servers over protocols other than HTTP.

Passing a Request to a Proxied Server

When NGINX proxies a request, it sends the request to a specified proxied server, fetches the response, and sends it back to the client. It is possible to proxy requests to an HTTP server (another NGINX server or any other server) or a non-HTTP server (which can run an application developed with a specific framework, such as PHP or Python) using a specified protocol. Supported protocols include FastCGI, uwsgi, SCGI, and memcached.

To pass a request to an HTTP proxied server, the proxy_pass directive is specified inside a location. For example:

location /some/path/ {    proxy_pass http://www.example.com/link/; }

This example configuration results in passing all requests processed in this location to the proxied server at the specified address. This address can be specified as a domain name or an IP address. The address may also include a port:

location ~ \.php {    proxy_pass http://127.0.0.1:8000; }

Note that in the first example above, the address of the proxied server is followed by a URI, /link/. If the URI is specified along with the address, it replaces the part of the request URI that matches the location parameter. For example, here the request with the /some/path/page.html URI will be proxied to http://www.example.com/link/page.html. If the address is specified without a URI, or it is not possible to determine the part of URI to be replaced, the full request URI is passed (possibly, modified).

To pass a request to a non-HTTP proxied server, the appropriate **_pass directive should be used:

Note that in these cases, the rules for specifying addresses may be different. You may also need to pass additional parameters to the server (see the reference documentation for more detail).

The proxy_pass directive can also point to a named group of servers. In this case, requests are distributed among the servers in the group according to the specified method.

Passing Request Headers

By default, NGINX redefines two header fields in proxied requests, “Host” and “Connection”, and eliminates the header fields whose values are empty strings. “Host” is set to the $proxy_host variable, and “Connection” is set to close.

To change these setting, as well as modify other header fields, use the proxy_set_header directive. This directive can be specified in a location or higher. It can also be specified in a particular servercontext or in the http block. For example:

location /some/path/ {    proxy_set_header Host $host;    proxy_set_header X-Real-IP $remote_addr;    proxy_pass http://localhost:8000; }

In this configuration the “Host” field is set to the $host variable.

To prevent a header field from being passed to the proxied server, set it to an empty string as follows:

location /some/path/ {    proxy_set_header Accept-Encoding "";    proxy_pass http://localhost:8000; }

Configuring Buffers

By default NGINX buffers responses from proxied servers. A response is stored in the internal buffers and is not sent to the client until the whole response is received. Buffering helps to optimize performance with slow clients, which can waste proxied server time if the response is passed from NGINX to the client synchronously. However, when buffering is enabled NGINX allows the proxied server to process responses quickly, while NGINX stores the responses for as much time as the clients need to download them.

The directive that is responsible for enabling and disabling buffering is proxy_buffering. By default it is set to on and buffering is enabled.

The proxy_buffers directive controls the size and the number of buffers allocated for a request. The first part of the response from a proxied server is stored in a separate buffer, the size of which is set with the proxy_buffer_size directive. This part usually contains a comparatively small response header and can be made smaller than the buffers for the rest of the response.

In the following example, the default number of buffers is increased and the size of the buffer for the first portion of the response is made smaller than the default.

location /some/path/ {    proxy_buffers 16 4k;    proxy_buffer_size 2k;    proxy_pass http://localhost:8000; }

If buffering is disabled, the response is sent to the client synchronously while it is receiving it from the proxied server. This behavior may be desirable for fast interactive clients that need to start receiving the response as soon as possible.

To disable buffering in a specific location, place the proxy_buffering directive in the location with the off parameter, as follows:

location /some/path/ {    proxy_buffering off;    proxy_pass http://localhost:8000; }

In this case NGINX uses only the buffer configured by proxy_buffer_size to store the current part of a response.

A common use of a reverse proxy is to provide load balancing. Learn how to improve power, performance, and focus on your apps with rapid deployment in the free Five Reasons to Choose a Software Load Balancer ebook.

Choosing an Outgoing IP Address

If your proxy server has several network interfaces, sometimes you might need to choose a particular source IP address for connecting to a proxied server or an upstream. This may be useful if a proxied server behind NGINX is configured to accept connections from particular IP networks or IP address ranges.

Specify the proxy_bind directive and the IP address of the necessary network interface:

location /app1/ {    proxy_bind 127.0.0.1;    proxy_pass http://example.com/app1/; }
 location /app2/ {    proxy_bind 127.0.0.2;    proxy_pass http://example.com/app2/; }

The IP address can be also specified with a variable. For example, the $server_addr variable passes the IP address of the network interface that accepted the request:

location /app3/ {    proxy_bind $server_addr;    proxy_pass http://example.com/app3/; }
코멘트 0
접기/펴기 | 댓글 새로고침
 
 
Total 683 articles in 22 / 57 pages
번호 제목 제목 조회 수 날짜날짜
431 [윈도우] 윈도우 하드를 다른 컴퓨터로 이동해서 사용하기 11599 2008/08/30
430 이 네트워크 리소스를 사용할 원한이 없습니다 라는 에러가 나올때 [1] 8241 2008/08/30
429 ASUS A8N-E 랜카드 드라이버가 잡히지 않을 때 7092 2008/09/14
428 [X61] x61 파티션 나누기 9460 2008/09/15
427 USB CD영역 만들기 (CD영역 생성하기, 시디영역 만들기) [2] 10000 2008/09/15
426 캐논 MF-4140 토너 구입기 4828 2008/09/18
425 IE7 새 탭을 열었습니다. 4595 2008/09/19
424 윈도우 Vista 동그라미 로딩 커서 Xp 에서 사용하기. 파일 4208 2008/09/21
423 비스타>계속 하려면 사용 권한이 필요합니다. 9546 2008/10/02
422 IE7 지연현상 해결법 2901 2008/10/04
421 [사이트관리] 구글 url 긴급 삭제 요청(구글 검색 삭제) [1] 9700 2008/10/09
420 무료문자보내기, extremeSMS 파일 5288 2008/11/25

해시태그 디렉터리

오늘의 핫게시물