安装Nginx
我们经常遇到一台服务器需要部署多个项目,这时候需要绑定多个域名或者使用二级域名访问指定项目,这时候需要用到Nginx的反向代理
通过yum安装
比较简答,一行命令就行
yum install nginx
安装后配置文件在/etc/nginx/nginx.conf
安装Tomcat
下载
通过命令下载,也可以到Tomcat官网下载后上传到服务器
# 切换到安装目录(这里使用/home/soft)
cd /home/soft
# 下载
wget http://mirrors.tuna.tsinghua.edu.cn/apache/tomcat/tomcat-8/v8.5.47/bin/apache-tomcat-8.5.47.zip
解压
unzip apache-tomcat-8.5.47.zip
配置Nginx
配置server
编辑http {}中的server,默认已经有一个,这里假设有两个项目,访问地址为http://demo.sample.top/和一个静态项目路径为/home/soft/demo/dosc访问地址为http://www.sample.top。
vi /etc/nginx/nginx.conf
修改http {}中的server配置
# 静态项目
server {
# 端口
listen 80;
# 域名
server_name www.sample.top;
# 编码
charset utf-8;
location / {
# 项目所在目录,如果目录不存在会报错
root /home/soft/demo/dosc;
# 首页文件
index index.html;
}
}
# Tomcat
server {
listen 80;
server_name demo.sample.top;
charset utf-8;
location / {
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_set_header X-NginX-Proxy true;
proxy_pass http://127.0.0.1:8080/;
}
}
启动Nginx
# 设置开机自启
systemctl enable nginx
# 启动
systemctl start nginx
以下为常用命令
# 停止
systemctl stop nginx
# 重启
systemctl restart nginx