>  기사  >  운영 및 유지보수  >  nginx 프록시 모듈을 사용하는 방법

nginx 프록시 모듈을 사용하는 방법

WBOY
WBOY앞으로
2023-05-17 10:01:051173검색

nginx 프록시 모듈

지침: 프록시 모듈에는 많은 지침이 있습니다. 여기서는 중요한 Proxy_pass에 대해서만 설명하겠습니다. 더 많은 프록시 지침을 알고 싶다면 공식 중국어 문서를 참조하세요.
이 모듈은 요청을 다른 서버로 전달할 수 있습니다. http/1.0은 keepalive를 사용할 수 없습니다(백엔드 서버는 각 요청에 대한 연결을 생성하고 삭제합니다). nginx는 브라우저에 대해 http/1.1을 보내고 백엔드 서버에 대해 http/1.0을 보내므로 브라우저는 브라우저에 대한 연결 유지를 처리할 수 있습니다.
아래 예:

location / {
 proxy_pass    http://localhost:8000;
 proxy_set_header x-real-ip $remote_addr;
}

http 프록시 모듈(또는 fastcgi)을 사용할 때 nginx는 모든 연결 요청을 백엔드 서버로 보내기 전에 캐시하므로 백엔드에서 전송되는 데이터를 측정할 때 진행 상황이 표시가 올바르지 않을 수 있습니다.

실험적 토폴로지:

nginx 프록시 모듈을 사용하는 방법

7. http 역방향 프록시 구성

[root@nginx ~]# cd /etc/nginx/
[root@nginx nginx]# cp nginx.conf nginx.conf.bak #备份一个原配置文件
[root@nginx nginx]# vim nginx.conf
location / {
        proxy_pass   http://192.168.18.201;
    }

명령 설명: Proxy_pass

구문: ​​Proxy_pass url

기본값: no

사용 필드:, 위치에서 if field

이 명령은 프록시 서버의 주소와 매핑된 URI를 설정합니다. 주소는 호스트 이름 또는 IP + 포트 번호 형식일 수 있습니다(예: Proxy_pass http://localhost:8000/uri/;
).

8. 구성 파일 다시 로드

[root@nginx ~]# service nginx reload
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
重新载入 nginx:                      [确定]

9. 테스트

nginx 프록시 모듈을 사용하는 방법

참고로 192.168.18.208에 액세스하면 프록시에 의해 web1로 리디렉션되는 것을 볼 수 있습니다.

10. 웹 서버 로그를 확인하세요

[root@web1 ~]# tail /var/log/httpd/access_log
192.168.18.208 - - [04/sep/2013:00:14:20 +0800] "get /favicon.ico http/1.0" 404 289 "-" "mozilla/5.0 (windows nt 6.1; wow64) applewebkit/537.36 (khtml, like gecko) chrome/28.0.1500.95 safari/537.36"
192.168.18.208 - - [04/sep/2013:00:14:20 +0800] "get / http/1.0" 200 23 "-" "mozilla/5.0 (windows nt 6.1; wow64) applewebkit/537.36 (khtml, like gecko) chrome/28.0.1500.95 safari/537.36"
192.168.18.208 - - [04/sep/2013:00:14:20 +0800] "get /favicon.ico http/1.0" 404 289 "-" "mozilla/5.0 (windows nt 6.1; wow64) applewebkit/537.36 (khtml, like gecko) chrome/28.0.1500.95 safari/537.36"
192.168.18.138 - - [04/sep/2013:00:14:45 +0800] "get / http/1.1" 200 23 "-" "mozilla/5.0 (windows nt 6.1; wow64) applewebkit/537.36 (khtml, like gecko) chrome/28.0.1500.95 safari/537.36"
192.168.18.138 - - [04/sep/2013:00:14:48 +0800] "get /favicon.ico http/1.1" 404 289 "-" "mozilla/5.0 (windows nt 6.1; wow64) applewebkit/537.36 (khtml, like gecko) chrome/28.0.1500.95 safari/537.36"
192.168.18.208 - - [04/sep/2013:00:14:55 +0800] "get /favicon.ico http/1.0" 404 289 "-" "mozilla/5.0 (windows nt 6.1; wow64) applewebkit/537.36 (khtml, like gecko) chrome/28.0.1500.95 safari/537.36"
192.168.18.208 - - [04/sep/2013:00:15:05 +0800] "get /favicon.ico http/1.0" 404 289 "-" "mozilla/5.0 (windows nt 6.1; wow64) applewebkit/537.36 (khtml, like gecko) chrome/28.0.1500.95 safari/537.36"
192.168.18.208 - - [04/sep/2013:00:15:13 +0800] "get /favicon.ico http/1.0" 404 289 "-" "mozilla/5.0 (windows nt 6.1; wow64) applewebkit/537.36 (khtml, like gecko) chrome/28.0.1500.95 safari/537.36"
192.168.18.208 - - [04/sep/2013:00:15:16 +0800] "get / http/1.0" 200 23 "-" "mozilla/5.0 (windows nt 6.1; wow64) applewebkit/537.36 (khtml, like gecko) chrome/28.0.1500.95 safari/537.36"
192.168.18.208 - - [04/sep/2013:00:15:16 +0800] "get /favicon.ico http/1.0" 404 289 "-" "mozilla/5.0 (windows nt 6.1; wow64) applewebkit/537.36 (khtml, like gecko) chrome/28.0.1500.95 safari/537.36"

참고로 여기 있는 고객의 IP는 모두 실제 클라이언트 IP가 아닌 nginx 프록시 서버의 IP임을 확인할 수 있습니다. 로그 IP가 실제 클라이언트 IP를 표시하도록 수정해 보겠습니다.

11. nginx 구성 파일 수정

location / {
    proxy_pass   http://192.168.18.201;
    proxy_set_header x-real-ip $remote_addr; #加上这一行
}

명령 설명: Proxy_set_header

구문: ​​Proxy_set_header 헤더 값

기본값: 호스트 및 연결

사용 필드: http, 서버, 위치

이 명령은 다음을 허용합니다. 프록시 서버로 전송되는 요청 헤더에 일부 필드를 재정의하거나 추가합니다. 이 값은 텍스트, 변수 또는 이들의 조합일 수 있습니다. Proxy_set_header는 지정된 필드에 정의되지 않은 경우 상위 필드에서 상속됩니다.

12. 구성 파일을 다시 로드합니다.

[root@nginx ~]# service nginx reload
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
重新载入 nginx:                      [确定]

13. 로그를 테스트하고 확인합니다

[root@web1 ~]# tail /var/log/httpd/access_log
192.168.18.208 - - [03/sep/2013:16:26:18 +0800] "get / http/1.0" 200 23 "-" "mozilla/5.0 (compatible; msie 10.0; windows nt 6.1; wow64; trident/6.0)"
192.168.18.208 - - [03/sep/2013:16:26:18 +0800] "get / http/1.0" 200 23 "-" "mozilla/5.0 (compatible; msie 10.0; windows nt 6.1; wow64; trident/6.0)"
192.168.18.208 - - [03/sep/2013:16:26:18 +0800] "get / http/1.0" 200 23 "-" "mozilla/5.0 (compatible; msie 10.0; windows nt 6.1; wow64; trident/6.0)"
192.168.18.208 - - [03/sep/2013:16:26:18 +0800] "get / http/1.0" 200 23 "-" "mozilla/5.0 (compatible; msie 10.0; windows nt 6.1; wow64; trident/6.0)"
192.168.18.208 - - [03/sep/2013:16:26:18 +0800] "get / http/1.0" 200 23 "-" "mozilla/5.0 (compatible; msie 10.0; windows nt 6.1; wow64; trident/6.0)"
192.168.18.208 - - [03/sep/2013:16:26:18 +0800] "get / http/1.0" 200 23 "-" "mozilla/5.0 (compatible; msie 10.0; windows nt 6.1; wow64; trident/6.0)"
192.168.18.208 - - [03/sep/2013:16:26:18 +0800] "get / http/1.0" 200 23 "-" "mozilla/5.0 (compatible; msie 10.0; windows nt 6.1; wow64; trident/6.0)"
192.168.18.208 - - [03/sep/2013:16:26:18 +0800] "get / http/1.0" 200 23 "-" "mozilla/5.0 (compatible; msie 10.0; windows nt 6.1; wow64; trident/6.0)"
192.168.18.208 - - [03/sep/2013:16:26:18 +0800] "get / http/1.0" 200 23 "-" "mozilla/5.0 (compatible; msie 10.0; windows nt 6.1; wow64; trident/6.0)"
192.168.18.208 - - [03/sep/2013:16:26:18 +0800] "get / http/1.0" 200 23 "-" "mozilla/5.0 (compatible; msie 10.0; windows nt 6.1; wow64; trident/6.0)"

로그에 여전히 프록시 IP가 기록되고 실제 클라이언트 IP가 표시되지 않는 것을 볼 수 있습니다. httpd 구성 파일을 살펴보겠습니다.

14.httpd 구성 파일 보기 및 수정

[root@web1 ~]# vim /etc/httpd/conf/httpd.conf

nginx 프록시 모듈을 사용하는 방법

여기서 로그를 기록할 수 있는 매개변수는 여전히 %h이므로 아래 매개변수를 수정해 보겠습니다.

nginx 프록시 모듈을 사용하는 방법

참고로 이것은 수정된 매개변수입니다. h%를 %{x-real-ip}i로 변경하세요. 좋습니다. 다시 테스트해 보겠습니다.

15. 다시 시작하고 테스트하세요

[root@web1 ~]# service httpd restart
停止 httpd:                        [确定]
正在启动 httpd:                      [确定]
[root@web1 ~]# tail /var/log/httpd/access_log
192.168.18.138 - - [03/sep/2013:17:09:14 +0800] "get / http/1.0" 200 23 "-" "mozilla/5.0 (compatible; msie 10.0; windows nt 6.1; wow64; trident/6.0)"
192.168.18.138 - - [03/sep/2013:17:09:14 +0800] "get / http/1.0" 200 23 "-" "mozilla/5.0 (compatible; msie 10.0; windows nt 6.1; wow64; trident/6.0)"
192.168.18.138 - - [03/sep/2013:17:09:15 +0800] "get / http/1.0" 200 23 "-" "mozilla/5.0 (compatible; msie 10.0; windows nt 6.1; wow64; trident/6.0)"
192.168.18.138 - - [03/sep/2013:17:09:15 +0800] "get / http/1.0" 200 23 "-" "mozilla/5.0 (compatible; msie 10.0; windows nt 6.1; wow64; trident/6.0)"
192.168.18.138 - - [03/sep/2013:17:09:15 +0800] "get / http/1.0" 200 23 "-" "mozilla/5.0 (compatible; msie 10.0; windows nt 6.1; wow64; trident/6.0)"
192.168.18.138 - - [03/sep/2013:17:09:15 +0800] "get / http/1.0" 200 23 "-" "mozilla/5.0 (compatible; msie 10.0; windows nt 6.1; wow64; trident/6.0)"
192.168.18.138 - - [03/sep/2013:17:09:15 +0800] "get / http/1.0" 200 23 "-" "mozilla/5.0 (compatible; msie 10.0; windows nt 6.1; wow64; trident/6.0)"
192.168.18.138 - - [03/sep/2013:17:09:15 +0800] "get / http/1.0" 200 23 "-" "mozilla/5.0 (compatible; msie 10.0; windows nt 6.1; wow64; trident/6.0)"
192.168.18.138 - - [03/sep/2013:17:09:15 +0800] "get / http/1.0" 200 23 "-" "mozilla/5.0 (compatible; msie 10.0; windows nt 6.1; wow64; trident/6.0)"
192.168.18.138 - - [03/sep/2013:17:09:15 +0800] "get / http/1.0" 200 23 "-" "mozilla/5.0 (compatible; msie 10.0; windows nt 6.1; wow64; trident/6.0)"

참고로 로그에 기록된 IP 주소가 실제 클라이언트 주소임을 알 수 있습니다.

위 내용은 nginx 프록시 모듈을 사용하는 방법의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!

성명:
이 기사는 yisu.com에서 복제됩니다. 침해가 있는 경우 admin@php.cn으로 문의하시기 바랍니다. 삭제