云服务器用nginx配置域名访问前后端分离项目

 更新时间:2025年09月16日 09:53:20   作者:柯南二号  
本文介绍通过Nginx反向代理实现多服务端口隐藏,统一域名入口访问,支持HTTPS自动跳转,用户只需输入域名即可访问前端、后端及其它服务,具有一定的参考价值,感兴趣的可以了解一下

云服务器有多个服务(前端 3000 端口、后端 8288 端口,甚至还有别的服务)。

希望用户只输入 域名(比如 https://example.com),而不是 example.com:3000example.com:8288

本质上是要做 端口隐藏 + 域名统一入口

核心方案是:用 Nginx / Caddy / Apache 作为反向代理(网关入口),监听标准端口 80/443,把请求转发到后端对应服务端口

🔧 实现方式:Nginx 反向代理

1. 安装 Nginx

(大多数云服务器镜像都内置了 Nginx,没有就 apt/yum install nginx

2. 配置虚拟主机

假设你有:

  • 前端 React/Vue 项目跑在 3000 端口
  • 后端 API 服务跑在 8288 端口

👉 你希望访问方式变成:

  • https://example.com/ → 前端页面
  • https://example.com/api/ → 后端 API

Nginx 配置:

server {
    listen 80;
    server_name example.com;

    # 前端(3000端口)
    location / {
        proxy_pass http://127.0.0.1:3000;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    }

    # 后端 API(8288端口)
    location /api/ {
        proxy_pass http://127.0.0.1:8288/;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    }
}

👉 用户访问 https://example.com/api/user,Nginx 会转发到 http://127.0.0.1:8288/user
👉 用户访问 https://example.com/,会自动转到 http://127.0.0.1:3000/

3. 其他服务端口同理

比如你还有 8080 端口的服务,可以映射为:

location /admin/ {
    proxy_pass http://127.0.0.1:8080/;
}

这样所有 非标准端口服务 都可以通过 域名 + 路径 来访问,不需要输入端口。

🔐 HTTPS 支持

再加上证书就能用 443 端口,避免浏览器提示不安全。
示例(Let’s Encrypt 免费证书):

server {
    listen 443 ssl;
    server_name example.com;

    ssl_certificate     /etc/letsencrypt/live/example.com/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem;

    location / {
        proxy_pass http://127.0.0.1:3000;
    }

    location /api/ {
        proxy_pass http://127.0.0.1:8288/;
    }
}

✅ 总结

一台服务器绑定多个端口的服务,要让用户只输入域名访问:

  1. 统一入口:80/443 端口(HTTP/HTTPS)。
  2. 用 Nginx/Apache/Caddy 做反向代理,把不同路径转发到不同端口。
  3. 用户永远只用域名,Nginx 在后台帮你隐藏端口。

整理一个 一键可用的 Nginx 配置模板,支持:

  • 前端项目(3000 端口)
  • 后端 API(8288 端口)
  • 其他服务(比如 8080 → /admin/)
  • 自动 HTTP → HTTPS 跳转
  • Let’s Encrypt 免费证书(只要申请一次即可)

🔧 完整 Nginx 配置示例

文件路径:/etc/nginx/sites-available/example.conf

# =============================
# HTTP 自动跳转到 HTTPS
# =============================
server {
    listen 80;
    server_name example.com;

    # 自动跳转 https
    return 301 https://$host$request_uri;
}

# =============================
# HTTPS 配置(主站)
# =============================
server {
    listen 443 ssl http2;
    server_name example.com;

    # SSL 证书(用 certbot/Let's Encrypt 获取)
    ssl_certificate     /etc/letsencrypt/live/example.com/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem;

    # 一些推荐的 SSL 配置
    ssl_protocols TLSv1.2 TLSv1.3;
    ssl_prefer_server_ciphers on;

    # =============================
    # 前端服务 (3000端口)
    # =============================
    location / {
        proxy_pass http://127.0.0.1:3000;
        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;
    }

    # =============================
    # 后端 API (8288端口)
    # =============================
    location /api/ {
        proxy_pass http://127.0.0.1:8288/;
        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;
    }

    # =============================
    # 其他服务 (8080端口)
    # =============================
    location /admin/ {
        proxy_pass http://127.0.0.1:8080/;
        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;
    }
}

🚀 使用步骤

  1. 安装 certbot 并申请证书
    (以 Ubuntu 为例)
sudo apt update
sudo apt install certbot python3-certbot-nginx -y
sudo certbot --nginx -d example.com
  1. 保存配置并启用
sudo ln -s /etc/nginx/sites-available/example.conf /etc/nginx/sites-enabled/
sudo nginx -t   # 检查配置是否正确
sudo systemctl reload nginx
  1. 测试访问
  • https://example.com/ → 前端(3000)
  • https://example.com/api/ → 后端(8288)
  • https://example.com/admin/ → 其他服务(8080)

✅ 从此以后,用户访问时 永远只需要域名,端口完全隐藏。

👍 这个场景就是典型的 前后端分离部署方案

  • 前端 (React) → 打包后是纯静态文件,放 Nginx 上跑。
  • 后端 (Spring Boot) → 打成 jar 包,跑在后台(一般是 8080/8288 端口)。
  • Nginx → 负责做 反向代理统一入口,屏蔽后端端口。

拆解完整步骤 👇

🚀 前后端分离项目部署流程

1. 部署 React 前端

  1. 本地打包:

    npm run build
    

    生成 build/ 目录。

  2. 上传到服务器:

    scp -r build/* root@your_server_ip:/var/www/react-app/
    
  3. React 访问路径问题:

    • 如果前端路由用了 BrowserRouter,一定要在 Nginx 配置里加:

      try_files $uri /index.html;
      

      否则刷新会 404

2. 部署 Spring Boot 后端

  1. 打包 jar:

    mvn clean package -DskipTests
    

    生成 xxx.jar

  2. 上传服务器:

    scp target/xxx.jar root@your_server_ip:/var/www/springboot/
    
  3. 启动 Spring Boot(建议后台运行 + 日志输出):

    nohup java -jar /var/www/springboot/xxx.jar --server.port=8288 > app.log 2>&1 &
    

    👉 这样后端就跑在 http://127.0.0.1:8288

(如果要长期运行,推荐用 systemdDocker 部署。)

3. 配置 Nginx

编辑 /etc/nginx/sites-available/project.conf

server {
    listen 80;
    server_name example.com;

    # ==================
    # 前端 React 静态资源
    # ==================
    root /var/www/react-app;
    index index.html;

    location / {
        try_files $uri /index.html;
    }

    # ==================
    # 后端 SpringBoot API
    # ==================
    location /api/ {
        proxy_pass http://127.0.0.1:8288/;   # 反向代理到 Spring Boot
        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;
    }
}

4. 启用配置 & 重启 Nginx

sudo ln -s /etc/nginx/sites-available/project.conf /etc/nginx/sites-enabled/
sudo nginx -t
sudo systemctl reload nginx

5. 配置 HTTPS(推荐)

使用 Let’s Encrypt 免费证书:

sudo apt install certbot python3-certbot-nginx -y
sudo certbot --nginx -d example.com

然后配置会自动加上 443,强制跳转到 https

✅ 最终效果

  • https://example.com/ → React 前端页面
  • https://example.com/api/... → Spring Boot 后端接口
  • 用户只需要记住 域名,端口全隐藏,前后端统一由 Nginx 分发。

到此这篇关于云服务器用nginx配置域名访问前后端分离项目的文章就介绍到这了,更多相关nginx 域名访问前后端分离内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家! 

相关文章

  • Ubuntu16.04上为Nginx创建自签名SSL证书

    Ubuntu16.04上为Nginx创建自签名SSL证书

    SSL证书是实现HTTPS的关键组成部分,本文主要介绍了Ubuntu16.04上为Nginx创建自签名SSL证书,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2024-05-05
  • Nginx 运维之域名验证的方法示例

    Nginx 运维之域名验证的方法示例

    这篇文章主要介绍了Nginx 运维之域名验证的方法示例,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2018-10-10
  • Nginx如何配置根据路径转发详解

    Nginx如何配置根据路径转发详解

    Nginx是作为一个反向代理,转发,和负载均衡的服务器,也可以用于分布式,下面这篇文章主要给大家介绍了关于Nginx如何配置根据路径转发的相关资料,文中通过实例代码介绍的非常详细,需要的朋友可以参考下
    2022-07-07
  • Nginx如何配置多个服务域名解析共用80端口详解

    Nginx如何配置多个服务域名解析共用80端口详解

    对于Web而已,80端口和443端口是十分重要的,下面这篇文章主要给大家介绍了关于Nginx如何配置多个服务域名解析共用80端口的相关资料,文中通过实例代码介绍的非常详细,需要的朋友可以参考下
    2022-09-09
  • Nginx配置如何区分PC或手机访问不同域名

    Nginx配置如何区分PC或手机访问不同域名

    这篇文章主要介绍了Nginx配置如何区分PC或手机访问不同域名,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2018-10-10
  • Nginx中proxy_pass指令斜杠的作用及说明

    Nginx中proxy_pass指令斜杠的作用及说明

    这篇文章主要介绍了Nginx中proxy_pass指令斜杠的作用及说明,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教
    2024-05-05
  • Nginx限制某个IP同一时间段的访问次数和请求数示例代码

    Nginx限制某个IP同一时间段的访问次数和请求数示例代码

    nginx可以通过ngx_http_limit_conn_module和ngx_http_limit_req_module配置来限制ip在同一时间段的访问次数.具体示例代码大家参考下本文
    2017-08-08
  • Nginx部署HTTP/3的实现步骤

    Nginx部署HTTP/3的实现步骤

    本文介绍了在Nginx中部署HTTP/3的详细步骤,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2025-09-09
  • Filebeat 采集 Nginx 日志的方法

    Filebeat 采集 Nginx 日志的方法

    这篇文章主要介绍了Filebeat 采集 Nginx 日志的方法,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2020-10-10
  • 关于nginx 实现jira反向代理的问题

    关于nginx 实现jira反向代理的问题

    这篇文章主要介绍了关于nginx 实现jira反向代理的问题,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2021-09-09

最新评论