19 安装nginx代理tomcat
为了在Nginx代理后面安装Tomcat,你需要先安装Nginx和Tomcat。以下是在基于Debian的系统(如Ubuntu)上进行安装的示例步骤:
- 安装Nginx:
sudo apt update
sudo apt install nginx
- 安装Java(如果还没有安装):
sudo apt install default-jdk
- 下载并解压Tomcat:
wget https://dlcdn.apache.org/tomcat/tomcat-9/v9.0.65/bin/apache-tomcat-9.0.65.tar.gz
sudo mkdir /opt/tomcat
sudo tar -xvf apache-tomcat-9.0.65.tar.gz -C /opt/tomcat --strip-components=1
- 创建一个服务文件来管理Tomcat:
sudo nano /etc/systemd/system/tomcat.service
添加以下内容:
[Unit]
Description=Tomcat 9 servlet container
After=network.target
[Service]
Type=forking
Environment=JAVA_HOME=/usr/lib/jvm/default-java
Environment=CATALINA_PID=/opt/tomcat/temp/tomcat.pid
Environment=CATALINA_HOME=/opt/tomcat
Environment=CATALINA_BASE=/opt/tomcat
Environment='CATALINA_OPTS=-Xms512M -Xmx1024M -server -XX:+UseParallelGC'
Environment='JAVA_OPTS=-Djava.awt.headless=true -Djava.security.egd=file:/dev/./urandom'
ExecStart=/opt/tomcat/bin/startup.sh
ExecStop=/opt/tomcat/bin/shutdown.sh
User=tomcat
Group=tomcat
UMask=0007
RestartSec=10
Restart=always
[Install]
WantedBy=multi-user.target
- 启动Tomcat服务并设置开机自启:
sudo systemctl daemon-reload
sudo systemctl start tomcat
sudo systemctl enable tomcat
- 配置Nginx来作为代理服务器。编辑Nginx配置文件:
sudo nano /etc/nginx/sites-available/default
在该文件中,修改或添加以下内容:
server {
listen 80;
location / {
proxy_pass http://localhost: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;
}
}
- 重启Nginx以应用更改:
sudo systemctl restart nginx
现在,你应该能够通过Nginx访问Tomcat服务器,Nginx将作为代理服务器转发请求到Tomcat的8080端口。
评论已关闭