同一个服务器绑定多个域名,利用Nginx进行域名重定向
背景:同一个服务器绑定多个域名,利用Nginx进行域名重定向。
首先:tomcat进行部署网站后台接口,进行重设9090端口(可根据需要自定义,但是注意外网访问端口,需在阿里云上配置开放端口)。
Tomcat版本8.044,Nginx版本nginx-1.14.1
Tomcat配置文件如下:
<Host appBase="webapps" autoDeploy="true" name="localhost" unpackWARs="true">
<Alias>域名1</Alias>
<Alias>域名2</Alias>
<Context path="" docBase="\mscs" debug="0" reloadable="True" />
<Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs" pattern="%h %l %u %t "%r" %s %b" prefix="localhost_access_log" suffix=".txt"/>
</Host>
将项目java接口项目导出war包,放在webapps即可
Nginx配置如下:
server {
listen 80;
server_name 域名2 域名1 ;
if ( $host != '域名2' ) {
rewrite ^/(.*)$ http:// 域名2/$1 permanent;
}
location / {
root html/nginx中前端项目名称;
index index.html index.htm;
}
location /tomcat中后台项目名称1/ {
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_pass http://127.0.0.1:9090;
}
location /tomcat中后台项目名称2/ {
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_pass http://127.0.0.1:9090;
}
location = /50x.html {
root html;
}
}
}
将网站部署在nginx下面,
对应nginx.conf 配置
Nginx进行域名重定向,域名2和域名1都重定向到域名2上
进入nginx命令窗口
进入到nginx.conf中 shift 按住 点击右键
Nginx 常用操作命令:
停止:nginx.exe -s stop
杀掉进程:taskkill /F /IM nginx.exe > nul
更新:nginx.exe -s reload
启动:start nginx