尚硅谷nginx教程-4nginx配置示例-反向代理

1. 反向代理实例1

目标

打开浏览器,在浏览器输入地址:www.123.com 跳转到Linux服务器的主页面中

1.1 准备工作

  • 1.安装tomcat并启动,端口号默认8080。
  • 2.修改hosts文件。因为没有域名,就在本地做配置,让它转发到nginx中,nginx再去请求tomcat
    * Hosts文件主要作用是定义IP地址和主机名的映射关系,是一个映射IP地址和主机名的规定。可以用文本文件打开!当用户在浏览器中输入网址时,系统会首先自动从Hosts文件中寻找对应的IP地址,一旦找到,浏览器会立即打开对应网页,如果没有找到,则浏览器会将网址提交DNS服务器进行IP地址解析。
# hosts文件加入IP地址和域名
IP地址	www.123.com

1.2 配置nginx

    server {
        listen       80;
        server_name  IP地址;    #1.将localhost改成IP地址

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            root   html;
            proxy_pass  http://127.0.0.1:8080;    #2.配置转发地址
            index  index.html index.htm;
        }

如上配置,我们监听80端口,而通过域名直接访问的80端口。通过nginx能给它转发到内部的8080端口上去。而浏览器不能直接访问8080(需要对外暴露端口才能访问),要通过80转发过去,这个过程成为请求转发。

1.3 验证

浏览器访问www.123.com

2.反向代理实例2

目标:使用nginx反向代理,根据访问的路径跳转到不同端口的服务中。
nginx监听端口为9001
访问http://IP地址:9001/edu/a.html,直接跳转到127.0.0.1:8081
访问http://IP地址:9001/vod/a.html,直接跳转到127.0.0.1:8080

2.1准备工作

  1. 在原来的tomcat/webapps/下,新建目录/edu,并创建a.html
<!DOCTYPE html>
<html>
<body>
<h1>/edu/8080</h1>
</body>
</html>

2.重新解压一个tomcat,并在webapps创建/vod,创建a.html

<!DOCTYPE html>
<html>
<body>
<h1>/vod/8081</h1>
</body>
</html>

修改配置文件,将端口号改为8081,其他的端口十位改为1,然后启动tomcat

<?xml version="1.0" encoding="UTF-8"?>

<Server port="8015" shutdown="SHUTDOWN">
  <Listener className="org.apache.catalina.startup.VersionLoggerListener" />

  <Listener className="org.apache.catalina.core.AprLifecycleListener" SSLEngine="on" />

  <Listener className="org.apache.catalina.core.JreMemoryLeakPreventionListener" />
  <Listener className="org.apache.catalina.mbeans.GlobalResourcesLifecycleListener" />
  <Listener className="org.apache.catalina.core.ThreadLocalLeakPreventionListener" />

 
  <GlobalNamingResources>
 
    <Resource name="UserDatabase" auth="Container"
              type="org.apache.catalina.UserDatabase"
              description="User database that can be updated and saved"
              factory="org.apache.catalina.users.MemoryUserDatabaseFactory"
              pathname="conf/tomcat-users.xml" />
  </GlobalNamingResources>


  <Service name="Catalina">

    <Connector port="8081" protocol="HTTP/1.1"
               connectionTimeout="20000"
               redirectPort="8443" />
    <Connector port="8019" protocol="AJP/1.3" redirectPort="8443" />

    <Engine name="Catalina" defaultHost="localhost">

      <Realm className="org.apache.catalina.realm.LockOutRealm">
        <Realm className="org.apache.catalina.realm.UserDatabaseRealm"
               resourceName="UserDatabase"/>
      </Realm>

      <Host name="localhost"  appBase="webapps"
            unpackWARs="true" autoDeploy="true">

        <Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"
               prefix="localhost_access_log" suffix=".txt"
               pattern="%h %l %u %t &quot;%r&quot; %s %b" />

      </Host>
    </Engine>
  </Service>
</Server>

2.2 修改nginx配置文件

在http块中添加如下配置

    server {
        listen  9001;
        server_name     IP地址;

        location ~ /edu/ {
                proxy_pass      http://127.0.0.1:8080;
        }
        location ~ /vod/ {
                proxy_pass      http://127.0.0.1:8081;
        }

2.3.验证

浏览器访问 http://IP地址:9001/vod/a.html, http://IP地址:9001/edu/a.html

3.location指令说明

该指令用于匹配URL,语法如下:

    location [ = | ~ | ~* | ^~ ] url{
    #    =:用于不含正则表达式的uri前,要求请求字符串与uri严格匹配,如果匹配成功,就停止继续向下搜索,并立即处理请求。
    #    ~:用于表示uri包含正则表达式,并且区分大小写。
    #    ~*:用于表示uri包含正则表达式,并且不区分大小写
    #    ^~:用于不含正则表达式的uri前,要求nginx服务器找到表示uri和请求字符串匹配度最高的location后,立即使用此location处理请求,而不再使用location块中的正则uri和请求字符串做匹配。
    }

注意:如果uri包含正则表达式,则必须要有~或者~*标示

原文地址:https://www.cnblogs.com/csj2018/p/12661518.html