Nginx反向代理微服务配置

admin
2022-03-14 / 0 评论 / 510 阅读 / 正在检测是否收录...
温馨提示:
本文最后更新于2022年03月26日,已超过761天没有更新,若内容或图片失效,请留言反馈。

Nginx反向代理微服务配置

一、本地域名服务访问过程

二、正向代理和反向代理

三、Nginx+Windows搭建域名访问环境

1、修改C:\Windows\System32\drivers\etc下hosts文件,映射本地域名和192.168.56.10ip关系。

每次通过记事本修改很麻烦,可以通过SwitchHosts软件修改。

192.168.56.10 family.com

2、现在就可以通过family.com域名访问了。

想要访问部署在服务器上的其它端口应用也可以通过域名访问,比如:搭建的kibana是5601端口,通过域名http://family.com:5601/即可访问了(kibana时部署在56.10服务器上的)

四、nginx.conf


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;
}

Nginx配置文件说明:

1、全局块:

配置影响nginx全局的指令。如:用户组、nginx进程pid存放路径,日志存放路径,配置文件引入,允许生成wordker process数等。

2、events块:

配置影响nginx服务器或用户的网络链接。如:每个进程的最大连接数,选取那种事件驱动模型处理链接请求,是否允许同时接收多个网络链接,开启多个网络链接序列化等。

3、http块:

可以嵌套多个server,配置代理,缓存,日志定义等绝大多数功能和第三方模块的配置。如文件引入,mime-type定义,是否使用sendfile传输文件,链接超时事件,单链接请求数等。

​ http块里面又包含:

3.1、http全局块:如upstream,错误页面,连接超时等。

3.2、server块: 配置虚拟注解的相关参数,一个http中可以有多个wever。

​ server块里面又包含:

​ 3.2.1、location:配置请求的路由,以及各种页面的处理情况。

注意: server块里面可以包含多个location。

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

注意:会包含所有include /etc/nginx/conf.d/这个目录下的所有配置文件。

比如/etc/nginx/conf.d/默认有个default.conf配置文件,里面就只包含了server块。

server {
    listen       80;   -----------nginx监听的端口
    server_name  localhost;  ------------nginx监听的端口

    #charset koi8-r;  
    #access_log  /var/log/nginx/log/host.access.log  main;  ----访问日志

    location / {  ---------------- /表示上面监听域名下所有的请求,都可以在root目录下找,包括首页index
        root   /usr/share/nginx/html
        index  index.html index.htm; 
    } 

    #error_page  404              /404.html; ----------404错误页面路径配置

    # redirect server error pages to the static page /50x.html
    #
    error_page   500 502 503 504  /50x.html;  ---------500页面路径配置
    location = /50x.html {
        root   /usr/share/nginx/html;
    }

五、反向代理配置

配置实例:

因为上面修改了host文件映射了域名到192.168.56.10服务器,所有通过服务上nginx访问本地127.0.0.1的springboot项目就可以通过配置反向代理域名访问。

将通过nginx访问family.com的所有请求,通过nginx反向代理到本地的springboot项目地址本地springboot项目ip192.168.56.1:。

server {
    listen       80;
    server_name  family.com;

    #charset koi8-r;
    #access_log  /var/log/nginx/log/host.access.log  main;

    location / {
        proxy_pass   http://192.168.56.1:40000;
    }

    #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;
    }

    # 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;
    #}
}

梳理流程:

1、修改本地host文件,目的是模拟类似访问外网的一个域名,如果有服务器域名绑定了ip,就可以不用这步骤了。

2、修改host映射family.com ------->192.168.56.10 (服务地址)。

3、访问family.com就相当于访问192.168.56.10 (服务地址)。

4、将所有family.com(这个域名默认端口为80)的所有请求,通过nginx反向代理到局域网内的其它服务器,比如这里就是本地搭建的springboot项目端口40000。

5、通过反向代理配置

listen       80; ------监控哪个端口
server_name  family.com; ------监听哪个域名
    
location / {   ---- /:表示所有请求
        proxy_pass   http://192.168.56.1:40000; --将family.com域名80端,反向代理到http://192.168.56.1:40000
}

6、现在family.com:80端口的所有请求就会反向代理到http://192.168.56.1:40000了。

反向代理后访问地址:

七、nginx代理网关

如果我们服务多了,每次都要修改域名,修改nginx配置反向代理到多个服务,这样比较麻烦。

那我们就可以通过配置nginx代理到微服务的网关,让网关来负载均衡到各服务。nginx负载均衡参考地址

nginx反向代理局域网内网关服务(192.168.56.1:88)。

nginx配置如下:


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;
    upstream family {
        server 192.168.56.1:88;
    }

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

注意修改的地方:

upstream family { ----网关取的名字
     server 192.168.56.1:88; -----网关服务地址
     server srv2.example.com; ----可以配置多个网关地址,我这只有一个网关配一个即可
     server srv3.example.com;
     server srv4.example.com;
}

修改yanxizhu.nginx配置

server {
    listen       80;
    server_name  family.com;

    #charset koi8-r;
    #access_log  /var/log/nginx/log/host.access.log  main;

    location / {
        proxy_pass   http://family;
    }

    #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;
    }

    # 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;
    #}
}

注意修改点:

location / {
        proxy_pass   http://family; ---这里就反向代理到family这个网关了,也就是上面取的网关名字
}

接着就开始配置getway网关服务请求

注意:nginx代理给网关的时候会丢失host信息。

解决办法;yanxizhu.conf添加代理host的header。

proxy_set_header Host $proxy_host;

family.conf整体如下:

server {
    listen       80;
    server_name  family.com;

    #charset koi8-r;
    #access_log  /var/log/nginx/log/host.access.log  main;

    location / {
        proxy_set_header Host $proxy_host;
        proxy_pass   http://family;
    }

    #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;
    }

    # 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;
    #}
}

getway网关服务负载均衡配置:

        - id: family_host_route
          uri: lb://family-booking
          predicates:
            - Host=**.family.com

比如访问http://family.com/时,nginx就会将请求反向代理给网关服务(192.168.56.1:88),而网关服务通过配置的负载均衡,又会将**.family.com域名的请求,负载均衡到family-booking微服务的80端口。

以上就是nginx的反向代理配置。注意将getway网关的配置,放在最后,不然全部服务都被代理到family_host_route了。

八、静态资源配置

将前端静态资源上传到nginx静态资源目录,后端主页面路径加上static路径。

server {
    listen       80;
    server_name  family.com;

    #charset koi8-r;
    #access_log  /var/log/nginx/log/host.access.log  main;
    
    location /static/ {
        root   /var/www/html; 
    }

    location / {
        proxy_set_header Host $proxy_host;
        proxy_pass   http://family;
    }

    #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;
    }

    # 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;
    #}
}

说明:

location /static/ {
    root   /var/www/html; 
}

后端主页访问静态资源都添加上/static/静态资源目录,当请求static资源路径时,会被nginx处理请求nginx下配置的静态资源。

11

评论 (0)

取消