location
/ios/
{
#这种情况,这里一定要匹配的是/ios/,不能是/ios
proxy_pass http:
//192
.168.1.102:8090;
#一定要保证192.168.1.102机器8090端口站点目录下有ios目录!否则访问会报错404!
proxy_redirect off ;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
1)
配置proxy_pass时,当在后面的url加上了/,相当于是绝对根路径,则nginx不会把location中匹配的路径部分代理走;
如果没有/,则会把匹配的路径部分也给代理走.
2) 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
!!!!
3)启动Nginx
[root@localhost ~]
# service nginx start //或者使用 systemctl start nginx.service
4)测试访问(103.110.186.23是192.168.1.23机器的外网ip)
[root@localhost conf.d]
# curl http://192.168.1.23
this is page of
test
!!!!
[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
[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]
# 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
>
7)第二种情况,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
那么访问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/
[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
9)第四种情况:相对于第三种配置的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
//192
.168.1.23
/proxy/
,后面就算是默认的index.html文件也要跟上,否则访问失败!
上面四种方式都是匹配的path路径后面加"/",下面说下path路径后面不带"/"的情况:
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
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]
#
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]
# 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
原文:https://www.cnblogs.com/k8s-pod/p/13912072.html