Fork me on GitHub

nginx的反向代理

nginx是很强大的一种反向代理服务器,本文是说nginx的反向代理。

1、正向代理和反向代理:

正向代理:客户端向目标服务器无法向某服务器发送请求,于是客户端先将请求发送至第三方服务器(代理服务器),让第三方服务器转发至目标服务器,这样目标服务器就不知道真正的客户端是我,只会以为代理服务器是客户端,这样是不安全的,因为真正的服务器是处于暴露情况下的。

反向代理:客户端向代理服务器发送请求,此服务器收到请求后,将请求转发至真正的服务器,对于客户端来说,它以为真正的服务器就是代理服务器,这样真正的服务器就是出于不暴露状态,比较安全。

两种代理的区别就是:

正向代理的话,客户端知道真正的服务器地址,反而服务器不知道真正的客户端地址

反向代理的话,客户端不知道真正的服务器地址,只知道代理服务器的地址,服务器可以通过代理服务器知道发送请求的客户端是谁,前提是只有一台反向代理服务器,如果存在多台反向代理服务器,服务器也不一定知道真正的客户端是谁(需要一层一层向上追查IP)。

 

2、Linux上配置nginx的反向代理:

 (1)将nginx压缩包上传至Linux服务器上;

 (2)解压 tar -zxvf  XXX

 (3)安装nginx所需要的配置包和创建快捷方式:

  注意:先安装nginx所需要的配置包,再创建快捷方式,如果先创建快捷方式,就会报错:

  ./configure: error: the HTTP rewrite module requires the PCRE library.
  You can either disable the module by using --without-http_rewrite_module
  option, or install the PCRE library into the system, or build the PCRE library
  statically from the source with nginx by using --with-pcre=<path> option.              

   安装所需配置包:

yum -y install gcc pcre-devel openssl openssl-devel

  创建快捷方式:

  在nginx的目录下

  ./configure --prefix=/usr/locl/nginx-1.17.3

  看到以下信息说明创建成功:
  Configuration summary
  + using system PCRE library
  + OpenSSL library is not used
  + using system zlib library

  nginx path prefix: "/usr/local/nginx-1.17.3"
  nginx binary file: "/usr/local/nginx-1.17.3/sbin/nginx"
  nginx modules path: "/usr/local/nginx-1.17.3/modules"
  nginx configuration prefix: "/usr/local/nginx-1.17.3/conf"
  nginx configuration file: "/usr/local/nginx-1.17.3/conf/nginx.conf"
  nginx pid file: "/usr/local/nginx-1.17.3/logs/nginx.pid"
  nginx error log file: "/usr/local/nginx-1.17.3/logs/error.log"
  nginx http access log file: "/usr/local/nginx-1.17.3/logs/access.log"
  nginx http client request body temporary files: "client_body_temp"
  nginx http proxy temporary files: "proxy_temp"
  nginx http fastcgi temporary files: "fastcgi_temp"
  nginx http uwsgi temporary files: "uwsgi_temp"
  nginx http scgi temporary files: "scgi_temp"

  (4)编译nginx:

      在nginx的目录:

      make && make install

      看到以下信息说明编译成功:
      make[1]: Leaving directory `/home/apps/nginx-1.17.3'

  (5)启动nginx:

      不再nginx的解压目录操作了,

      可以去快捷方式的目录去执行了(/usr/locl/nginx-1.17.3)

    ./nginx(在sbin目录下),如没有报错说明成功

  (6)使用浏览器进行检测:

      URL输入当前nginx服务器的IP,页面会出现Welcome to nginx!

 

 

反向代理的配置:

    准备一台web服务器  IP为151

 1、到/usr/local/nginx-1.17.3的config中进行配置
  vim nginx.conf
注意:
  必须要在http{}标签里面实现所有的配置
  upstream tomcat_server(名字随便起,开心就好){
  server 192.168.23.151:8080;(分号不要忘了!!!!)
}
  upstream必须要配置在server{}标签的上面
  有加载顺序:
  如果配置在了server的下面,最终因为加载顺序缘故(自上而下的加载顺序),server中加载不到upstream,则就会报错!!!!
  配置server{}标签中location /{}标签
  把location /{}标签中的所有内容全部删除
  location / {
  proxy_pass http://tomcat_server;(upstream的名字,必须要保持一致,否则无法找到映射)
  }
 2.重新nginx
  需要在sbin目录
  ./nginx -s reload

3、在151服务器上配置相应的Tomcat

4、检测nginx的反向代理是否成功

  在浏览器上访问150的nginx服务器的8080端口,nginx会将请求发送至151的服务器上,访问的是151的Tomcat

 

posted @ 2019-08-31 10:25  秋刀  阅读(2209)  评论(0编辑  收藏  举报