You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

DigitalOcean Tomcat部署WebApp后Nginx反向代理配置求助

配置Nginx反向代理到Tomcat Servlet应用的完整步骤

没问题,我来一步步帮你搞定这个配置——之前我在DigitalOcean上部署Java Web应用时也踩过类似的坑,按下面的步骤来应该就能解决:


1. 先确保Nginx已安装(如果还没装的话)

根据你的Droplet系统选择对应的安装命令:

  • Ubuntu/Debian 系统:
    sudo apt update && sudo apt install nginx -y
    
  • CentOS/RHEL 系统:
    sudo dnf install nginx -y
    

2. 创建专属的Nginx站点配置文件

不建议直接修改默认配置,新建一个专门的配置文件更便于维护:

sudo nano /etc/nginx/sites-available/server.foodini.co.in

把下面的配置内容粘贴进去,注意几个关键点:

  • 替换http://localhost:8080/Servelet里的Servelet为你实际的WebApp名称(一定要准确对应大小写,否则会出现404)
  • 如果你的Tomcat修改过默认端口(不是8080),要把端口号也改过来
server {
    listen 80;
    server_name server.foodini.co.in;

    # 反向代理所有请求到Tomcat的WebApp
    location / {
        proxy_pass http://localhost:8080/Servelet;
        # 传递必要的请求头,确保Tomcat能获取正确的客户端信息
        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;
    }
}

写完后按Ctrl+O保存,Ctrl+X退出编辑器。

3. 启用这个站点配置

把新建的配置文件软链接到Nginx的启用目录:

sudo ln -s /etc/nginx/sites-available/server.foodini.co.in /etc/nginx/sites-enabled/

4. 检查配置是否合法(这一步千万别跳过!)

配置错误会导致Nginx启动失败,先验证一下:

sudo nginx -t

如果输出nginx: configuration file /etc/nginx/nginx.conf test is successful,说明配置没问题;如果报错,根据提示修改配置文件即可。

5. 重启Nginx使配置生效

sudo systemctl restart nginx

额外排查项(如果还是无法访问的话)

  • Tomcat监听地址检查:打开Tomcat的server.xml文件(通常在/opt/tomcat/conf/,路径可能因安装方式不同而变化),找到Connector标签,确保address="0.0.0.0"(而不是localhost),这样Nginx才能访问到Tomcat服务。
  • 防火墙设置:确保80端口已经开放,Ubuntu系统可以用sudo ufw allow 80/tcp,CentOS系统用sudo firewall-cmd --permanent --add-port=80/tcp && sudo firewall-cmd --reload。
  • 域名解析验证:本地ping一下server.foodini.co.in,确认它指向你的Droplet公网IP。

内容的提问来源于stack exchange,提问作者Kushan

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.19 08:47:48