Nginx 配置反向代理

1、前言

反向代理做用

隐藏服务器信息 -> 保证内网的安全,一般将反向代理做为公网访问地址,web服务器是内网,即经过nginx配置外网访问web服务器内网html

举例

好比小编的码云我的博客地址为:http://zhengqingya.gitee.io/b... ,如今小编想经过本身的服务器地址 http://www.zhengqing520.com/b... 来访问到码云上面我的博客的地址,而且访问地址是本身的服务器ip或者域名地址,这时候咱们就能够经过Nginx配置反向代理来实现 ~nginx

2、Nginx如何配置反向代理呢?

咱们能够经过 proxy_pass 来配置git

(1)找到nginx配置文件 nginx.conf

舒适小提示

小编是经过docker拉取的nginx,默认配置文件是nginx.conf中引入包含的default.conf文件
也就是说nginx.conf配置文件中有以下一个配置web

include /etc/nginx/conf.d/*.conf;

(2)修改配置 -> 实现反向代理

注:这里小编将个人default.conf配置文件中的内容提到nginx.conf配置文件中来实现
即注释 include /etc/nginx/conf.d/*.conf;
简单配置

好比 www.zhengqing520.com 转发到 http://zhengqingya.gitee.iodocker

server {
    listen       80;
    server_name  www.zhengqing520.com;# 服务器地址或绑定域名

    location / { # 访问80端口后的全部路径都转发到 proxy_pass 配置的ip中
        root   /usr/share/nginx/html;
        index  index.html index.htm;
           proxy_pass http://zhengqingya.gitee.io; # 配置反向代理的ip地址和端口号 【注:url地址需加上http:// 或 https://】
    }
}

复杂配置

根据不一样的后缀名访问不一样的服务器地址后端

  1. www.zhengqing520.com/api 转发到 http://www.zhengqing520.com:9528/api/
  2. www.zhengqing520.com/blog/ 转发到 http://zhengqingya.gitee.io/b...
server {
    listen       80;
    server_name  www.zhengqing520.com;# 服务器地址或绑定域名
 
    location ^~ /api {  # ^~/api 表示匹配前缀为api的请求
        proxy_pass  http://www.zhengqing520.com:9528/api/;  # 注:proxy_pass的结尾有/, -> 效果:会在请求时将/api/*后面的路径直接拼接到后面
  
        # proxy_set_header做用:设置发送到后端服务器(上面proxy_pass)的请求头值  
            # 【当Host设置为 $http_host 时,则不改变请求头的值;
            #   当Host设置为 $proxy_host 时,则会从新设置请求头中的Host信息;
            #   当为$host变量时,它的值在请求包含Host请求头时为Host字段的值,在请求未携带Host请求头时为虚拟主机的主域名;
            #   当为$host:$proxy_port时,即携带端口发送 ex: $host:8080 】
        proxy_set_header Host $host; 
  
        proxy_set_header X-Real-IP $remote_addr; # 在web服务器端得到用户的真实ip 需配置条件①    【 $remote_addr值 = 用户ip 】
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;# 在web服务器端得到用户的真实ip 需配置条件②
        proxy_set_header REMOTE-HOST $remote_addr;
        # proxy_set_header X-Forwarded-For $http_x_forwarded_for; # $http_x_forwarded_for变量 = X-Forwarded-For变量
    }

    location ^~ /blog/ { # ^~/blog/ 表示匹配前缀为blog/后的请求
        proxy_pass  http://zhengqingya.gitee.io/blog/; 
  
        proxy_set_header Host $proxy_host; # 改变请求头值 -> 转发到码云才会成功
        proxy_set_header  X-Real-IP  $remote_addr;
        proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-NginX-Proxy true;
    }
}

3、总结

这里再给出一下小编nginx配置文件中的所有内容以供参考api

user  nginx;
worker_processes  1;

error_log  /var/log/nginx/error.log warn;
pid        /var/run/nginx.pid;

events {
    worker_connections  1024;
}

http {
    include       /etc/nginx/mime.types;
    default_type  application/octet-stream;

    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    access_log  /var/log/nginx/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    keepalive_timeout  65;

    #gzip  on;

    # include /etc/nginx/conf.d/*.conf; # 引入default.conf配置文件
  
    server {
        listen       80;
        server_name  www.zhengqing520.com;# 服务器地址或绑定域名

        #charset koi8-r;
        #access_log  /var/log/nginx/host.access.log  main;
        
        # start ---------------------------------------------------------------------------------------------
    
        location / {
            root   /usr/share/nginx/html;
            try_files $uri $uri/ @router;
            index  index.html index.htm;
            # proxy_pass http://zhengqingya.gitee.io; # 代理的ip地址和端口号
            # proxy_connect_timeout 600; #代理的链接超时时间(单位:毫秒)
            # proxy_read_timeout 600; #代理的读取资源超时时间(单位:毫秒)
        } 

        location @router {
            rewrite ^.*$ /index.html last;  
        }

        location ^~ /api {  # ^~/api/表示匹配前缀为api的请求
            proxy_pass  http://www.zhengqing520.com:9528/api/;  # 注:proxy_pass的结尾有/, -> 效果:会在请求时将/api/*后面的路径直接拼接到后面
      
            # proxy_set_header做用:设置发送到后端服务器(上面proxy_pass)的请求头值  
                # 【当Host设置为 $http_host 时,则不改变请求头的值;
                #   当Host设置为 $proxy_host 时,则会从新设置请求头中的Host信息;
                #   当为$host变量时,它的值在请求包含Host请求头时为Host字段的值,在请求未携带Host请求头时为虚拟主机的主域名;
                #   当为$host:$proxy_port时,即携带端口发送 ex: $host:8080 】
            proxy_set_header Host $host; 
      
            proxy_set_header X-Real-IP $remote_addr; # 在web服务器端得到用户的真实ip 需配置条件①    【 $remote_addr值 = 用户ip 】
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;# 在web服务器端得到用户的真实ip 需配置条件②
            proxy_set_header REMOTE-HOST $remote_addr;
            # proxy_set_header X-Forwarded-For $http_x_forwarded_for; # $http_x_forwarded_for变量 = X-Forwarded-For变量
        }
    
        location ^~ /blog/ { # ^~/blog/ 表示匹配前缀为blog/后的请求
            proxy_pass  http://zhengqingya.gitee.io/blog/;   
      
            proxy_set_header Host $proxy_host; # 改变请求头值 -> 转发到码云才会成功
            proxy_set_header  X-Real-IP  $remote_addr;
            proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header X-NginX-Proxy true;
        }
       
        # end ---------------------------------------------------------------------------------------------

        #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   /usr/share/nginx/html;
        }

   }
}
相关文章
相关标签/搜索