nginx搭配Spring Cloud Gateway 搭建域名访问环境

本文详细介绍了如何使用SwitchHosts管理本地host文件,配置Nginx作为反向代理服务器,以及Spring Cloud Gateway作为微服务网关的具体步骤。涵盖了Nginx配置文件的修改、域名监听设置、请求转发及请求头信息传递,以及Spring Cloud Gateway的路由配置等关键信息。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1. 安装SwitchHosts,方便切换本地host文件

安装链接:https://github.com/oldj/SwitchHosts/releases
在这里插入图片描述

2. 修改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;
    # 配置微服务网关的地址
    upstream gulimall{
        server 192.168.202.1:88;
    }
    # 为了防止 nginx.conf 文件膨胀过大,因此采用该方法引入其他配置文件
    include /etc/nginx/conf.d/*.conf;
}

3. 修改 /conf.d/default.conf 文件
server {
	# 监听来自 gulimall.com 域名的 80 端口的请求
	# 这里的域名与 SwitchHosts 里面配置的域名一致
    listen       80;
    server_name  gulimall.com;

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

    location / {
    	# proxy_pass 将监听到的请求转发到 nginx.conf 文件中的 对应的upstream 中,即 192.168.202.1:88 (网关地址)。即在此完成了访问网址-> nginx -> 网关
        proxy_pass http://gulimall;
        # proxy_set_header 给通过 nginx 的请求添加请求头的部分信息,这里需要注意,经过 nginx 的请求会丢失请求头的部分信息。这里添加域名信息,方便在网关处拦截并转发到对应的微服务。
        proxy_set_header Host $host;
    }

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

4. Spring Cloud GateWay 配置
spring:
  cloud:
    gateway:
      routes:   
        - id: gulimall_host_route
          uri: lb://guli-mall-product
          predicates:
            - Host=**.gulimall.com

注意将该拦截条件放在最后,以免影响通过该域名的 api 的调用

### Spring Cloud Gateway 配置 Nginx 的最佳实践 在构建微服务架构时,Spring Cloud GatewayNginx 是两种常用的工具。Spring Cloud Gateway 作为 API 网关,负责路由、过滤和安全控制;而 Nginx 则主要用作反向代理和负载均衡器。以下是配置 NginxSpring Cloud Gateway 的最佳实践。 #### 1. Nginx 作为反向代理的配置 Nginx 可以将外部请求转发到内部的 Spring Cloud Gateway 实例。以下是一个典型的 Nginx 配置示例: ```nginx server { listen 80; server_name your-domain.com; location / { proxy_pass http://localhost:8080; # Spring Cloud Gateway 的地址 proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header X-Forwarded-Proto $scheme; } # SSL 配置 listen 443 ssl; ssl_certificate /etc/nginx/ssl/your-domain.crt; ssl_certificate_key /etc/nginx/ssl/your-domain.key; } ``` 此配置中,Nginx 监听 80 和 443 端口,并将所有请求转发到本地运行的 Spring Cloud Gateway 实例[^2]。 #### 2. Spring Cloud Gateway 的基本配置 Spring Cloud Gateway配置通常通过 `application.yml` 文件完成。以下是一个简单的配置示例: ```yaml spring: cloud: gateway: routes: - id: service_a_route uri: http://service-a:8081 predicates: - Path=/service-a/** filters: - StripPrefix=1 - id: service_b_route uri: http://service-b:8082 predicates: - Path=/service-b/** filters: - StripPrefix=1 ``` 此配置定义了两个路由规则,分别将 `/service-a/**` 和 `/service-b/**` 的请求转发到对应的服务[^1]。 #### 3. 使用 Docker 部署 Spring Cloud Gateway 为了简化部署过程,可以使用 Docker 容器化 Spring Cloud Gateway。以下是一个 Docker 命令示例: ```bash docker run --name gateway \ --rm \ -p 8080:8080 \ -e "SPRING_PROFILES_ACTIVE=prod" \ springcloud/spring-cloud-gateway:latest ``` 此命令启动了一个 Spring Cloud Gateway 容器,并将其绑定到主机的 8080 端口[^3]。 #### 4. 配合 NginxSpring Cloud Gateway 的最佳实践 - **性能优化**:确保 Nginx 的 `worker_processes` 和 `worker_connections` 参数根据服务器硬件资源进行调整。 - **SSL/TLS 支持**:始终为外部流量启用 HTTPS,以保护用户数据的安全。 - **日志记录**:配置 NginxSpring Cloud Gateway 的日志记录功能,以便于问题排查。 - **健康检查**:通过 Nginx 或其他工具定期检查 Spring Cloud Gateway 的健康状态。 - **负载均衡**:如果多个 Spring Cloud Gateway 实例运行在同一环境中,可以使用 Nginx 的 `upstream` 模块实现负载均衡。 #### 5. 示例教程总结 通过上述配置,可以实现一个高效的微服务网关系统。Nginx 作为反向代理和负载均衡器,Spring Cloud Gateway 负责路由和过滤逻辑。两者结合可以显著提高系统的可扩展性和安全性。 ---
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值