nginx Proxy_pass 역방향 프록시 구성 예제 분석

王林
풀어 주다: 2023-05-13 23:19:11
앞으로
1145명이 탐색했습니다.

다음은 간단한 예입니다.

centos7 시스템 라이브러리에는 기본적으로 nginx rpm 패키지가 없으므로 먼저 rpm 종속성 라이브러리를 업데이트해야 합니다.

1) yum을 사용하여 nginx를 설치하려면 nginx를 포함해야 합니다. library , nginx 라이브러리 설치

[root@localhost ~]# rpm -uvh http://nginx.org/packages/centos/7/noarch/rpms/nginx-release-centos-7-0.el7.ngx.noarch.rpm
로그인 후 복사

2) 다음 명령을 사용하여 nginx

[root@localhost ~]# yum install nginx
로그인 후 복사

3) nginx 구성

[root@localhost ~]# cd /etc/nginx/conf.d/
[root@localhost conf.d]# cat test.conf
server {
listen 80;
server_name localhost;
location / {
root /var/www/html;
index index.html;
}
}
 
[root@localhost conf.d]# cat /var/www/html/index.html
this is page of test!!!!
로그인 후 복사

4) nginx 시작

[root@localhost ~]# service nginx start //或者使用 systemctl start nginx.service
로그인 후 복사

5) 테스트 액세스(103.110.186.23은 192.168입니다.) .1.23 머신 외부 네트워크 IP)

[root@localhost conf.d]# curl http://192.168.1.23
this is page of test!!!!
로그인 후 복사

다음 상황을 살펴보십시오. http://192.168.1.23/proxy/index.html을 사용하여 액세스 테스트를 수행하세요

테스트의 편의를 위해 먼저 다른 머신 192.168을 사용하십시오. .1.5 포트 8090에 nginx를 배포합니다. 구성은 다음과 같습니다.

[root@bastion-idc ~]# cat /usr/local/nginx/conf/vhosts/haha.conf
server {
listen 8090;
server_name localhost;
location / {
root /var/www/html;
index index.html;
}
}
[root@bastion-idc ~]# cat /var/www/html/index.html
this is 192.168.1.5
[root@bastion-idc ~]# /usr/local/nginx/sbin/nginx -s reload
로그인 후 복사

테스트 액세스(103.110.186.5는 192.168.1.5의 외부 네트워크 IP입니다.):

[root@bastion-idc ~]# curl http://192.168.1.5:8090
this is 192.168.1.5
로그인 후 복사

nginx proxy_pass反向代理配置实例分析

192.168.1.23은 는 nginx 역방향 프록시 머신, nginx 구성은 다음과 같습니다.

1) 첫 번째 경우:

[root@localhost conf.d]# cat test.conf
server {
listen 80;
server_name localhost;
location / {
root /var/www/html;
index index.html;
}
 
location /proxy/ {
 proxy_pass http://192.168.1.5:8090/;
}
}
로그인 후 복사

이렇게 하면 http://192.168.1.23/proxy/에 대한 액세스가 http://192.168에 대한 프록시가 됩니다. .1.5:8090/. p와 일치하는 프록시 디렉토리는 루트 디렉토리 /var/www/html

에 존재할 필요가 없습니다. 터미널에서 http://192.168.1.23/proxy에 액세스하는 경우(즉, 뒤에 "/"가 없음) ), 액세스가 실패합니다! Proxy_pass

[root@localhost conf.d]# curl http://192.168.1.23/proxy/
this is 192.168.1.5
[root@localhost conf.d]# curl http://192.168.1.23/proxy
<html>
<head><title>301 moved permanently</title></head>
<body bgcolor="white">
<center><h1>301 moved permanently</h1></center>
<hr><center>nginx/1.10.3</center>
</body>
</html>
로그인 후 복사

에서 설정한 url 뒤에 "/"가 추가되기 때문에 페이지가 http://103.110.186.23/proxy에 접속하면 자동으로 "/"가 추가됩니다. (같은 이유는 url 뒤에 "/"가 추가되기 때문입니다. proxy_pass로 구성), http://103.110.186.5:8090

nginx proxy_pass反向代理配置实例分析

2)의 결과로 역전됩니다. 두 번째 경우, Proxy_pass

[root@localhost conf.d]# cat test.conf
server {
listen 80;
server_name localhost;
location / {
root /var/www/html;
index index.html;
}
 
location /proxy/ {
 proxy_pass http://192.168.1.5:8090;
}
}
[root@localhost conf.d]# service nginx restart
redirecting to /bin/systemctl restart nginx.service
로그인 후 복사

로 구성한 URL 뒤에 "/"가 추가되지 않으면 다음을 방문하세요. http://192.168.1.23/proxy 또는 http://192.168.1.23/proxy/는 실패합니다!

이 구성 후 http://192.168.1.23/proxy/에 대한 액세스는 http://192.168.1.5:8090/proxy/

nginx proxy_pass反向代理配置实例分析

3) 세 번째 상황

[root@localhost conf.d]# cat test.conf
server {
listen 80;
server_name localhost;
location / {
root /var/www/html;
index index.html;
}
 
location /proxy/ {
 proxy_pass http://192.168.1.5:8090/haha/;
}
}
[root@localhost conf.d]# service nginx restart
redirecting to /bin/systemctl restart nginx.service
[root@localhost conf.d]# curl http://192.168.1.23/proxy/
192.168.1.5 haha-index.html
로그인 후 복사

이렇게 구성한 경우 http://103.110.186.23/proxy를 방문하여 http://192.168.1.5:8090/haha/

nginx proxy_pass反向代理配置实例分析

4)로 프록시 설정합니다. 4) 네 번째 상황: 세 번째 구성의 URL에 상대적 "/" 추가

[root@localhost conf.d]# cat test.conf
server {
listen 80;
server_name localhost;
location / {
root /var/www/html;
index index.html;
}
 
location /proxy/ {
 proxy_pass http://192.168.1.5:8090/haha;
}
}
[root@localhost conf.d]# service nginx restart
redirecting to /bin/systemctl restart nginx.service
[root@localhost conf.d]# curl http://192.168.1.23/proxy/index.html
192.168.1.5 hahaindex.html
로그인 후 복사

위 구성 후 http://192.168.1.23/proxy/index.html에 액세스하면 http://192.168.1.5:8090/hahaindex.html
로 프록시가 됩니다. 마찬가지로 http:/에 액세스합니다. /192.168.1.23/proxy/test.html은 http://192.168.1.5:8090/hahatest.html

[root@localhost conf.d]# curl http://192.168.1.23/proxy/index.html
192.168.1.5 hahaindex.html
로그인 후 복사

로 프록시됩니다. 이 경우 http://192.168은 1.23/proxy에 직접 액세스할 수 없습니다. /, 기본 index.html 파일도 유지되어야 합니다. 그렇지 않으면 액세스가 실패합니다!

nginx proxy_pass反向代理配置实例分析

------------------------------- ------ -----------------
위 4가지 경로 뒤에 "/"를 추가합니다. 경로 뒤에 "/"가 없는 상황에 대해 이야기해 보겠습니다.

1) 첫 번째 경우, Proxy_pass 뒤의 URL에 "/"가 있습니다:

[root@localhost conf.d]# cat test.conf
server {
listen 80;
server_name localhost;
location / {
root /var/www/html;
index index.html;
}
 
location /proxy {
 proxy_pass http://192.168.1.5:8090/;
}
}
[root@localhost conf.d]# service nginx restart
redirecting to /bin/systemctl restart nginx.service
로그인 후 복사

nginx proxy_pass反向代理配置实例分析

nginx proxy_pass反向代理配置实例分析

2 ) 두 번째 경우, Proxy_pass 뒤의 url에 "/"

[root@localhost conf.d]# cat test.conf
server {
listen 80;
server_name localhost;
location / {
root /var/www/html;
index index.html;
}
 
location /proxy {
 proxy_pass http://192.168.1.5:8090;
}
}
[root@localhost conf.d]# service nginx restart
redirecting to /bin/systemctl restart nginx.service
[root@localhost conf.d]#
로그인 후 복사

가 포함되어 있지 않은 경우, 이렇게 구성하면 http://103.110.186.23/proxy에 접속하면 자동으로 "/"가 추가됩니다(즉, http://103.110.186.23/proxy/), 192.168.1.5:8090/proxy/

nginx proxy_pass反向代理配置实例分析

3) 세 번째 경우

[root@localhost conf.d]# cat test.conf
server {
listen 80;
server_name localhost;
location / {
root /var/www/html;
index index.html;
}
 
location /proxy {
 proxy_pass http://192.168.1.5:8090/haha/;
}
}
[root@localhost conf.d]# service nginx restart
redirecting to /bin/systemctl restart nginx.service
로그인 후 복사

이렇게 구성하면 http://103.110에 접속하게 됩니다. .186.23/proxy는 "/"(즉, http://103.110.186.23/proxy/가 됨), http://192.168.1.5:8090/haha/

nginx proxy_pass反向代理配置实例分析

4)에 대한 프록시를 자동으로 추가합니다. 네 번째 상황: 세 번째 구성과 비교하면 URL이 다릅니다. "/"

[root@localhost conf.d]# cat test.conf
server {
listen 80;
server_name localhost;
location / {
root /var/www/html;
index index.html;
}
 
location /proxy {
 proxy_pass http://192.168.1.5:8090/haha;
}
}
[root@localhost conf.d]# service nginx restart
redirecting to /bin/systemctl restart nginx.service
로그인 후 복사

nginx proxy_pass反向代理配置实例分析

이렇게 구성하면 세 번째 결과와 마찬가지로 http://103.110.186.23/proxy에 액세스합니다. http://192.168.1.5:8090/haha/

로 프록시되었습니다.

위 내용은 nginx Proxy_pass 역방향 프록시 구성 예제 분석의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!

관련 라벨:
원천:yisu.com
본 웹사이트의 성명
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.
인기 튜토리얼
더>
최신 다운로드
더>
웹 효과
웹사이트 소스 코드
웹사이트 자료
프론트엔드 템플릿
회사 소개 부인 성명 Sitemap
PHP 중국어 웹사이트:공공복지 온라인 PHP 교육,PHP 학습자의 빠른 성장을 도와주세요!