环境变量可不可以是一个具体变量nginxproxy_pass反向代理配置中url后加不加的区别介绍前⾔
nginx作为web服务器⼀个重要的功能就是反向代理。nginx反向代理的指令不需要新增额外的模块,默认⾃带proxy_pass指令,只需要修改配置⽂件就可以实现反向代理。
⽽在⽇常的web⽹站部署中,经常会⽤到nginx的proxy_pass反向代理,有⼀个配置需要弄清楚:配置proxy_pass时,当在后⾯的url加上了/,相当于是绝对根路径,则nginx不会把location中匹配的路径部分代理⾛;如果没有/,则会把匹配的路径部分也给代理⾛(这样配置可以参考)。
下⾯举个⼩实例说明下:
centos7系统库中默认是没有nginx的rpm包的,所以我们⾃⼰需要先更新下rpm依赖库
1)使⽤yum安装nginx需要包括Nginx的库,安装Nginx的库
[root@localhost ~]# rpm -Uvh /packages/centos/7/noarch/RPMS/arch.rpm
2)使⽤下⾯命令安装nginx
[root@localhost ~]# yum install nginx
3)nginx配置
[root@localhost ~]# cd /etc/nginx/conf.d/
[root@localhost conf.d]# f
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 192.168.1.23
this is page of test
为了⽅便测试,先在另⼀台机器192.168.1.5上部署⼀个8090端⼝的nginx,配置如下:
[root@bastion-IDC ~]# cat /usr/local/nginx/conf/f
server {
listen 8090;
server_name localhost;
location / {
root /var/www/html;
index index.html;java随机出题
}
}
[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 192.168.1.5:8090
this is 192.168.1.5
192.168.1.23作为nginx反向代理机器,nginx配置如下:
1)第⼀种情况:
[root@localhost conf.d]# f
server {
listen 80;
position和location的区别server_name localhost;
location / {
root /var/www/html;
index index.html;
}
location /proxy/ {
proxy_pass 192.168.1.5:8090/;
plot函数只有一个参数时}
}
[root@localhost conf.d]# curl 192.168.1.23/proxy/ this is 192.168.1.5
[root@localhost conf.d]# curl 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>
2)第⼆种情况,proxy_pass配置的url后⾯不加"/" [root@localhost conf.d]# f
server {
listen 80;
server_name localhost;
location / {
root /var/www/html;
index index.html;
}
location /proxy/ {
proxy_pass 192.168.1.5:8090;
}
}
[root@localhost conf.d]# service nginx restart Redirecting to /bin/systemctl restart nginx.service
3)第三种情况
[root@localhost conf.d]# f
server {
listen 80;
server_name localhost;
location / {
root /var/www/html;
index index.html;
}
location /proxy/ {
proxy_pass 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 192.168.1.23/proxy/ 192.168.1.5 haha-index.html
4)第四种情况:相对于第三种配置的url不加"/" [root@localhost conf.d]# f
server {
listen 80;
server_name localhost;
location / {
root /var/www/html;
index index.html;
}
location /proxy/ {
proxy_pass 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 192.168.1.23/proxy/index.html
192.168.1.5 hahaindex.html
[root@localhost conf.d]# curl 192.168.1.23/proxy/index.html
192.168.1.5 hahaindex.html
-------------------------------------------------------------------------------------
上⾯四种⽅式都是匹配的path路径后⾯加"/",下⾯说下path路径后⾯不带"/"的情况:1)第⼀种情况,proxy_pass后⾯url带"/":
[root@localhost conf.d]# f
server {
listen 80;
server_name localhost;
location / {
root /var/www/html;
index index.html;
}
location /proxy {
proxy_pass 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]# f
server {
listen 80;
server_name localhost;
location / {
root /var/www/html;
index index.html;
}
location /proxy {
proxy_pass 192.168.1.5:8090;
}
}
dropdownstyle
after怎么读[root@localhost conf.d]# service nginx restart
Redirecting to /bin/systemctl restart nginx.service
[root@localhost conf.d]#
3)第三种情况
[root@localhost conf.d]# f
server {
listen 80;
server_name localhost;
location / {
root /var/www/html;
index index.html;
}
location /proxy {
proxy_pass 192.168.1.5:8090/haha/;
}
}
[root@localhost conf.d]# service nginx restart
Redirecting to /bin/systemctl restart nginx.service
4)第四种情况:相对于第三种配置的url不加"/"
[root@localhost conf.d]# f
server {
listen 80;
server_name localhost;
location / {
root /var/www/html;
index index.html;
}
location /proxy {
proxy_pass 192.168.1.5:8090/haha;
}
}
[root@localhost conf.d]# service nginx restart
Redirecting to /bin/systemctl restart nginx.service
总结
以上就是这篇⽂章的全部内容了,希望本⽂的内容对⼤家的学习或者⼯作具有⼀定的参考学习价值,如果有疑问⼤家可以留⾔交流,谢谢⼤家对的⽀持。

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。