Rumah  >  Artikel  >  Operasi dan penyelenggaraan  >  nginx proxy_pass analisis contoh konfigurasi proksi terbalik

nginx proxy_pass analisis contoh konfigurasi proksi terbalik

王林
王林ke hadapan
2023-05-13 23:19:111183semak imbas

Berikut ialah contoh kecil:

Tiada pakej rpm nginx dalam pustaka sistem centos7 secara lalai, jadi kami perlu mengemas kini perpustakaan pergantungan rpm sendiri terlebih dahulu

1) Menggunakan yum untuk memasang nginx memerlukan termasuk pustaka nginx Pasang pustaka nginx

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

2) Gunakan arahan berikut untuk memasang nginx

[root@localhost ~]# yum install nginx
<.>3) konfigurasi 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) Mulakan nginx


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

5) Akses ujian (103.110.186.23 ialah IP rangkaian luaran mesin 192.168.1.23).


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

Lihat situasi berikut: gunakan http://192.168.1.23/proxy/index.html untuk menjalankan ujian akses

Untuk memudahkan ujian, ujian pertama pada komputer lain Gunakan nginx dengan port 8090 pada mesin 192.168.1.5 Konfigurasi adalah seperti berikut:

[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

Akses ujian (103.110.186.5 ialah IP rangkaian luaran 192.168.1.5):


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

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

192.168.1.23 sebagai mesin proksi terbalik nginx, konfigurasi nginx adalah seperti berikut:

1) Kes pertama:

[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/;
}
}

Dengan cara ini, akses kepada http://192.168.1.23/proxy/ akan menjadi proksi kepada http://192.168.1.5:8090/. Direktori proksi padanan p tidak perlu wujud dalam direktori akar /var/www/html


Perhatikan bahawa jika anda mengakses http://192.168.1.23/proxy dalam terminal (iaitu, tanpa "/" selepasnya) , akses akan gagal! Kerana "/" ditambah selepas url dikonfigurasikan oleh proxy_pass "/"), dan berbalik kepada hasil http://103.110.186.5:8090

2) Dalam kes kedua, jangan tambah URL selepas konfigurasi proxy_pass "/"nginx 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>

Kemudian mengakses http://192.168.1.23/proxy atau http://192.168.1.23/proxy/ akan gagal!

Selepas konfigurasi ini, akses kepada http://192.168.1.23/proxy/ akan menjadi proksi terbalik kepada http://192.168.1.5:8090/proxy/

nginx proxy_pass反向代理配置实例分析3) Kes ketiga

[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

Jika dikonfigurasikan dengan cara ini, akses http://103.110.186.23/proksi ke http://192.168.1.5:8090/ haha /

4) Situasi keempat: Berbanding konfigurasi ketiga, url tidak menambah "/"nginx 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/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

Selepas konfigurasi di atas, akses http ://192.168.1.23/proxy/index.html akan menjadi proksi kepada http://192.168.1.5:8090/hahaindex.html

Begitu juga, lawati http://192.168.1.23/proxy/test.html Ia akan proksi ke 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

Perhatikan bahawa dalam kes ini, anda tidak boleh mengakses terus http://192.168.1.23/proxy/, yang akan diikuti kemudian Malah fail index.html lalai mesti mengikuti, jika tidak akses akan gagal!

nginx proxy_pass反向代理配置实例分析-------------------------------- ------------------------------------------------- ---
Empat kaedah di atas semuanya menambah "/" selepas laluan padanan Berikut ialah kes di mana laluan tidak diikuti oleh "/":

1) Kes pertama, proxy_pass The. url berikut mempunyai "/":

[root@localhost conf.d]# curl http://192.168.1.23/proxy/index.html
192.168.1.5 hahaindex.html

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

2) Dalam kes kedua, url selepas proxy_pass tidak mempunyai " /" nginx 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

Jika dikonfigurasikan dengan cara ini, mengakses http://103.110.186.23/proxy secara automatik akan menambah "/" (iaitu, ia menjadi http://103.110.186.23/proxy/), dan proksinya ialah 192.168.1.5: 8090/proxy/

3) Kes ketiga nginx 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
[root@localhost conf.d]#

Jika dikonfigurasikan dengan cara ini, akses ke http:/ /103.110.186.23/proxy akan ditambah secara automatik "/" (iaitu, ia menjadi http://103.110.186.23/proxy/), proksi kepada http://192.168.1.5:8090/haha/

4) Situasi keempat: Berbanding dengan konfigurasi ketiga URL tanpa menambah "/"nginx 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/haha/;
}
}
[root@localhost conf.d]# service nginx restart
redirecting to /bin/systemctl restart nginx.service

, jika dikonfigurasikan dalam ini cara, akses http://103.110.186.23/proxy, dan Hasil ketiga adalah sama, ia juga diproksi ke http://192.168.1.5:8090/haha/nginx proxy_pass反向代理配置实例分析

Atas ialah kandungan terperinci nginx proxy_pass analisis contoh konfigurasi proksi terbalik. Untuk maklumat lanjut, sila ikut artikel berkaitan lain di laman web China PHP!

Kenyataan:
Artikel ini dikembalikan pada:yisu.com. Jika ada pelanggaran, sila hubungi admin@php.cn Padam