Heim  >  Artikel  >  Betrieb und Instandhaltung  >  Beispielanalyse der Nginx-Proxy_Pass-Reverse-Proxy-Konfiguration

Beispielanalyse der Nginx-Proxy_Pass-Reverse-Proxy-Konfiguration

王林
王林nach vorne
2023-05-13 23:19:111183Durchsuche

Das Folgende ist ein kleines Beispiel:

In der Centos7-Systembibliothek gibt es standardmäßig kein Nginx-RPM-Paket, daher müssen wir zuerst die RPM-Abhängigkeitsbibliothek aktualisieren.

1) Die Verwendung von yum zur Installation von Nginx erfordert die Einbindung von Nginx Bibliothek, installieren Sie die Nginx-Bibliothek 3 ist die 192.168 .1.23-Maschine Externe Netzwerk-IP)

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


Sehen Sie sich die folgenden Situationen an: Verwenden Sie http://192.168.1.23/proxy/index.html, um Zugriffstests durchzuführen


Um das Testen zu vereinfachen, verwenden Sie zunächst eine andere Maschine 192.168.1.23 .1.5 Stellen Sie einen Nginx auf Port 8090 bereit. Die Konfiguration lautet wie folgt:

[root@localhost ~]# yum install nginx

Testzugriff (103.110.186.5 ist die externe Netzwerk-IP von 192.168.1.5):

[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!!!!


192.168.1 .23 dient als Nginx-Reverse-Proxy-Maschine, Nginx Die Konfiguration ist wie folgt:

1) Der erste Fall:

[root@localhost ~]# service nginx start //或者使用 systemctl start nginx.service

Auf diese Weise wird der Zugriff auf http://192.168.1.23/proxy/ ein Proxy für http://192.168 sein .1.5:8090/. Das mit p übereinstimmende Proxy-Verzeichnis muss nicht im Stammverzeichnis /var/www/html vorhanden sein

nginx proxy_pass反向代理配置实例分析
Beachten Sie dies, wenn Sie im Terminal auf http://192.168.1.23/proxy zugreifen (d. h. ohne „/“ danach). ), schlägt der Zugriff fehl! Da „/“ nach der durch „proxy_pass“ konfigurierten URL hinzugefügt wird

[root@localhost conf.d]# curl http://192.168.1.23
this is page of test!!!!

Wenn die Seite auf http://103.110.186.23/proxy zugreift, wird „/“ automatisch hinzugefügt (derselbe Grund ist, dass „/“ nach der URL hinzugefügt wird konfiguriert durch „proxy_pass“) und umgekehrt zum Ergebnis von http://103.110.186.5:8090

2) Im zweiten Fall, wenn „/“ nicht nach der durch „proxy_pass“ konfigurierten URL hinzugefügt wird

[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

, dann besuchen Sie http://192.168.1.23/proxy oder http://192.168.1.23/proxy/ wird fehlschlagen!

Nach dieser Konfiguration ist der Zugriff auf http://192.168.1.23/proxy/ ein Reverse-Proxy zu http://192.168.1.5:8090/proxy/

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

3) Die dritte Situation

[root@bastion-idc ~]# curl http://192.168.1.5:8090
this is 192.168.1.5

Wenn Sie so konfiguriert sind, besuchen Sie http://103.110.186.23/proxy, um einen Proxy zu http://192.168.1.5:8090/haha/


4) Die vierte Situation: relativ zur URL der dritten Konfiguration Ohne Hinzufügen von „/“

[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/;
}
}
nginx proxy_pass反向代理配置实例分析Nach der obigen Konfiguration wird der Zugriff auf http://192.168.1.23/proxy/index.html ein Proxy für http://192.168.1.5:8090/hahaindex.html sein
Ähnlich auch der Zugriff auf http :/ /192.168.1.23/proxy/test.html wird an http://192.168.1.5:8090/hahatest.html weitergeleitet. Beachten Sie, dass in diesem Fall nicht direkt auf http://192.168 zugegriffen werden kann. 1.23/proxy / muss auch die Standarddatei index.html folgen, sonst schlägt der Zugriff fehl!

----------------------------------------------------------- ------ ----------------------------------------nginx proxy_pass反向代理配置实例分析Die oben genannten vier Methoden stimmen alle überein. Fügen Sie „/“ nach dem Pfad hinzu. Sprechen wir über die Situation ohne „/“ nach dem Pfad:

1) Im ersten Fall hat die URL nach „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>



nginx proxy_pass反向代理配置实例分析2) Im zweiten Fall, wenn die URL nach „proxy_pass“ nicht „/“ enthält

[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

, wird beim Zugriff auf http://103.110.186.23/proxy bei einer solchen Konfiguration automatisch „/“ hinzugefügt (d. h. es wird zu http://103.110.186.23/proxy/), Proxy zu 192.168.1.5:8090/proxy/


3) Der dritte Fall

[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
nginx proxy_pass反向代理配置实例分析Wenn auf diese Weise konfiguriert, wird auf http://103.110 zugegriffen .186.23/proxy fügt automatisch „/“ hinzu (das heißt, es wird http://103.110.186.23/proxy/), Proxy für http://192.168.1.5:8090/haha/

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

4) Das Vierte Situation: Im Vergleich zur dritten Konfiguration lautet die URL nicht „/“ hinzufügen

Wenn sie so konfiguriert ist, wird auf http://103.110.186.23/proxy zugegriffen. Wie beim dritten Ergebnis wird auch ein Proxy verwendet http://192.168.1.5:8090/haha/

Das obige ist der detaillierte Inhalt vonBeispielanalyse der Nginx-Proxy_Pass-Reverse-Proxy-Konfiguration. Für weitere Informationen folgen Sie bitte anderen verwandten Artikeln auf der PHP chinesischen Website!

Stellungnahme:
Dieser Artikel ist reproduziert unter:yisu.com. Bei Verstößen wenden Sie sich bitte an admin@php.cn löschen