nginx服务器反向代理

前言:

        反向代理(Reverse Proxy)方式是指以代理服务器来接受internet上的连接请求,然后将请求转发给内部网络上的服务器(通过端口映射实现),并将从服务器上得到的结果返回给internet上请求连接的客户端,此时代理服务器对外就表现为一个服务器。

环境描述:反向代理服务器为Linux系统,安装有Nginx;web服务器为windows系统,装有IIS

目标:客户端在访问https://qa.nfzr365.com的时候得到的内容必须是192.168.66.62这台web服务器上的内容


首先我们在Nginx上设置反向代理,修改Nginx配置文件,将如下部分删除

server {

    #listen 80;
    listen 443 ssl;

    server_name  qa.nfzr365.com;
    index index.html index.php;
    #root /usr/local/nginx/www/Shop2.0/api/public;  //注释掉原有路径

    ssl_certificate      cert/1524033480643.pem;
    ssl_certificate_key  cert/1524033480643.key;
    ssl_session_timeout 5m;
    ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4;
    ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
    ssl_prefer_server_ciphers on;
   #-----注释掉部分-----#
   # location / {
   #         try_files $uri $uri/ /index.php?$query_string;
   # } 
#-----注释掉部分-----#
#-----新增部分-----# location / { proxy_pass http://117.131.6.52:88; proxy_redirect off; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; } #-----新增部分------#
 
 
#-----注释掉部分-----#
# location ~ \.php { # fastcgi_pass 127.0.0.1:9000; # fastcgi_index index.php; # include fastcgi.conf; # fastcgi_split_path_info ^(.+\.php)(.*)$; # fastcgi_param PATH_INFO /usr/local/nginx/www/Shop2.0/api/public$fastcgi_path_info;
#-----注释掉部分-----#

  删除完成后添加内容如下:    server name 在真实环境中需要设置域名,因为我们实际访问网站很少用IP,proxy_pass 为web服务器的地址,如web服务器的网站用的是8080端口,那么我们的 proxy_pass 的格式就是 http://web网站IP或者域名:88

   server
        {
        listen          80;
        server_name    qa.nfzr365.com;    //反向代理服务器IP
        location / {
                proxy_pass              http://117.131.6.52:88;     //web服务器IP
                proxy_redirect          off;
                proxy_set_header        X-Real-IP       $remote_addr;
                proxy_set_header        X-Forwarded-For $proxy_add_x_forwarded_for;
                }
        #error_page  404              /404.html;

 设置完成后执行/usr/bin/nginx -t 检查配置是否正常,如果显示:

nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok

nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful

 则正常,否则按错误提示进行修改(基本都是xxx.conf的报错)。检查配置文件无误后杀死Nginx进程,然后重新启动Nginx。这时候客户端访问https://qa.nfzr365.com这台服务器 得到的就是192.168.66.62服务器上面的内容了。

猜你喜欢

转载自blog.csdn.net/mr_jhon_sheng/article/details/80006250