0
点赞
收藏
分享

微信扫一扫

Nginx笔记3-Nginx配置示例-反向代理

龙毓七七 2023-03-07 阅读 111


普通的反向代理

# 跳转到/usr目录
[root@localhost ~]# cd /usr
# 下载Tomcat
[root@localhost usr]# wget https://mirrors.cnnic.cn/apache/tomcat/tomcat-8/v8.5.64/bin/apache-tomcat-8.5.64.tar.gz
# 解压缩
[root@localhost usr]# tar -zxvf apache-tomcat-8.5.64.tar.gz
# Tomcat需要JDK环境,没有JDK环境的还要先安装和配置JDK环境
# 启动Tomcat
[root@localhost usr]# cd apache-tomcat-8.5.64/bin/
[root@localhost bin]# ./startup.sh
# 查看启动日志
[root@localhost bin]# cd ../logs/
[root@localhost logs]# tail -f catalina.out

直接访问Tomcat,在浏览器中输入虚拟机IP:默认端口号,即可访问到Tomcat了。
下面通过Nginx的反向代理访问Tomcat。

# 启动Nginx
[root@localhost logs]# cd /usr/local/nginx/sbin
[root@localhost sbin]# ./nginx
# 修改Nginx的配置文件
[root@localhost sbin]# cd ../conf/
[root@localhost conf]# vim nginx.conf
# 重新加载Nginx
[root@localhost conf]# nginx -s reload

Nginx修改后的配置文件,只修改了server部分。

server {
listen 80;
# 修改server_name为Nginx服务器的ip
server_name 192.168.0.123;

#charset koi8-r;

#access_log logs/host.access.log main;

location / {
root html;
# 添加一个proxy_pass,指向Tomcat的访问路径
proxy_pass http://127.0.0.1:8080;
index index.html index.htm;
}

#error_page 404 /404.html;

# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}

# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}

# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
#location ~ \.php$ {
# root html;
# fastcgi_pass 127.0.0.1:9000;
# fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
# include fastcgi_params;
#}

# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}

根据请求路径做反向代理

这里要启动两台Tomcat,再搞出来一台,修改端口号,保证端口号不冲突,并启动。

[root@localhost ~]# cd /usr
[root@localhost usr]# cp -r apache-tomcat-8.5.64/ apache-tomcat-9090
[root@localhost usr]# cd apache-tomcat-9090/conf
[root@localhost conf]# vim server.xml
[root@localhost conf]# cd ../bin/
[root@localhost bin]# ./startup.sh

为了区分两台Tomcat,在每个Tomcat下创建一个测试页面。在8080的Tomcat的webapps目录下,创建一个8080的文件夹,放入一个test.html页面,同理,9090的Tomcat照做。
下面修改Nginx的配置文件,修改server部分,修改后的内容如下所示。

server {
listen 80;
server_name 192.168.0.123;
#charset koi8-r;
#access_log logs/host.access.log main;
# 当访问路径中有/8080/的时候,将请求转发到127.0.0.1:8080的服务
location ~ /8080/ {
proxy_pass http://127.0.0.1:8080;
}
# 当访问路径中有/9090/的时候,将请求转发到127.0.0.1:9090的服务
location ~ /9090/ {
proxy_pass http://127.0.0.1:9090;
}

#error_page 404 /404.html;

# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}

# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}

# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
#location ~ \.php$ {
# root html;
# fastcgi_pass 127.0.0.1:9000;
# fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
# include fastcgi_params;
#}

# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}

通过浏览器访问http://192.168.0.123/8080/test.html和http://192.168.0.123/9090/test.html,可以看到请求分别发送到了两台机器上。
最后再说一下location的语法:location [=|~|~*|^~|@] pattern { … }。
=:要求路径完全匹配,忽略请求路径后的参数
~:用于区分大小写的正则匹配
~*:用于不区分大小写的正则匹配
^~:普通字符匹配,优先于正则表达式匹配,如果该选项匹配成功,不再进行正则表达式的匹配,多用来目录匹配
@:定义一个命名的location,使用在内部定向
location 优先级官方文档

  1. Directives with the = prefix that match the query exactly. If found, searching stops.
  2. All remaining directives with conventional strings, longest match first. If this match used the ^~ prefix, searching stops.
  3. Regular expressions, in order of definition in the configuration file.
  4. If #3 yielded a match, that result is used. Else the match from #2 is used.

翻译:

  1. =前缀的指令严格匹配这个查询。如果找到,停止搜索。
  2. 所有剩下的常规字符串,最长的匹配。如果这个匹配使用^~前缀,搜索停止。
  3. 正则表达式,在配置文件中定义的顺序。
  4. 如果第3条规则产生匹配的话,结果被使用。否则,使用第2条规则的结果。


举报

相关推荐

0 条评论