Maison  >  Article  >  Opération et maintenance  >  Nginx proxy_pass analyse d'un exemple de configuration de proxy inverse

Nginx proxy_pass analyse d'un exemple de configuration de proxy inverse

王林
王林avant
2023-05-13 23:19:111183parcourir

Ce qui suit est un petit exemple :

Il n'y a pas de package RPM nginx dans la bibliothèque système centos7 par défaut, nous devons donc d'abord mettre à jour la bibliothèque de dépendances RPM

1) Utiliser yum pour installer nginx nécessite d'inclure le nginx bibliothèque, installez la bibliothèque nginx

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

2) Utilisez la commande suivante pour installer nginx

[root@localhost ~]# yum install nginx

3) Configuration 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) Démarrez nginx

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

5) Test d'accès (103.110.186.23 est le 192.1 68 .1.23 IP du réseau externe de la machine)

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

Regardez les situations suivantes : utilisez http://192.168.1.23/proxy/index.html pour effectuer des tests d'accès

Pour faciliter les tests, utilisez d'abord une autre machine 192.168 .1.5 Déployez un nginx sur le port 8090, la configuration est la suivante :

[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

Test d'accès (103.110.186.5 est l'IP du réseau externe de 192.168.1.5) : sert de nginx reverse proxy machine, nginx La configuration est la suivante :

nginx proxy_pass反向代理配置实例分析1) Le premier cas :

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

De cette façon, l'accès à http://192.168.1.23/proxy/ sera proxy vers http://192.168 .1.5:8090/. Le répertoire proxy correspondant à p n'a pas besoin d'exister dans le répertoire racine /var/www/html

Notez que si vous accédez à http://192.168.1.23/proxy dans le terminal (c'est-à-dire sans "/" après ), l'accès échouera ! Parce que "/" est ajouté après l'url configurée par 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/;
}
}

Lorsque la page accède à http://103.110.186.23/proxy, "/" sera automatiquement ajouté (la même raison est que "/" est ajouté après l'url configuré par proxy_pass ), et inverser le résultat de http://103.110.186.5:8090


2) Dans le deuxième cas, si "/" n'est pas ajouté après l'url configuré par 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>

alors visitez http://192.168.1.23/proxy ou http://192.168.1.23/proxy/ échouera !

Après cette configuration, l'accès à http://192.168.1.23/proxy/ sera inversé par proxy vers http://192.168.1.5:8090/proxy/nginx proxy_pass反向代理配置实例分析


3) La troisième situation

[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

Si configuré comme ceci, visitez http://103.110.186.23/proxy pour proxy vers http://192.168.1.5:8090/haha/

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

4) La quatrième situation : relative à l'URL de la troisième configuration Sans ajouter "/"

[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

Après la configuration ci-dessus, l'accès à http://192.168.1.23/proxy/index.html sera un proxy vers http://192.168.1.5:8090/hahaindex.html

De même, l'accès à http : //192.168.1.23/proxy/test.html sera proxy vers http://192.168.1.5:8090/hahatest.html

[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
nginx proxy_pass反向代理配置实例分析Notez que dans ce cas, http://192.168 n'est pas accessible directement 1.23/. proxy/, même le fichier index.html par défaut doit suivre, sinon l'accès échouera !



-------------------------------------------- ------ ---------------------------------------------

Les quatre ci-dessus les méthodes correspondent toutes Ajoutez "/" après le chemin. Parlons de la situation sans "/" après le chemin :

1) Dans le premier cas, l'url après proxy_pass a "/" :

[root@localhost conf.d]# curl http://192.168.1.23/proxy/index.html
192.168.1.5 hahaindex.html
nginx proxy_pass反向代理配置实例分析


2 ) Dans le deuxième cas, si l'url après proxy_pass ne contient pas "/"

[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反向代理配置实例分析, si configuré comme ceci, accéder à http://103.110.186.23/proxy ajoutera automatiquement "/" (c'est-à-dire il deviendra http://103.110.186.23/ proxy/), proxy vers 192.168.1.5:8090/proxy/

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

3) Le troisième cas

[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]#

Si configuré de cette manière, accéder à http://103.110 .186.23/proxy ajoutera automatiquement "/" (c'est-à-dire qu'il devient http://103.110.186.23/proxy/), proxy vers http://192.168.1.5:8090/haha/

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

4) Le quatrième situation : Par rapport à la troisième configuration, l'URL n'est pas Ajouter "/"

[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

Si configurée comme ceci, accédez à http://103.110.186.23/proxy Comme le troisième résultat, elle sera également proxy vers. http://192.168.1.5:8090/haha/nginx proxy_pass反向代理配置实例分析

Ce qui précède est le contenu détaillé de. pour plus d'informations, suivez d'autres articles connexes sur le site Web de PHP en chinois!

Déclaration:
Cet article est reproduit dans:. en cas de violation, veuillez contacter admin@php.cn Supprimer