用jsp做的公司官网,在本地eclipse里面访问时的url为:http://localocalhost:8080/gateway/gateway/index
在阿里云服务器部署url:http://123456.com:8080/gateway/gateway/index
第一个gateway:服务名(tomcat的webapps目录下的软链)
第二个gateway:controller类上的mapping
领导要求:使用“123456.com”域名直接访问官网,不要端口及后面的部分
注:“123456.com”为举例使用的域名
工作点:
1、去掉端口:可以改tomcat端口,也可以使用nginx监听80端口(本处例子用的nginx监听80端口,另阿里云需要开放80端口)
2、去掉端口后面的一串:更改web.xml配置,设置“欢迎页”为首页controller方法(需要配合springmvc一起用)
3、“123456.com”和“www.123456.com”有无www都要能访问:先在域名服务商处增加两个映射到阿里云服务器,再使用nginx监听两个域名的访问并重定向
以下为代码部分:
控制层代码:
@Controller
@RequestMapping("gateway")
public class GatewayController {
@RequestMapping("/index")
public String index(HttpServletRequest request, HttpServletResponse response, ModelMap model) {
return "modules/index";
}
}
springMvc配置:
web.xml配置:
SpringMVC
org.springframework.web.servlet.DispatcherServlet
contextConfigLocation
classpath:springMVC.xml
1
SpringMVC
/
SpringMVC
/gateway/index
SpringMVC
*.mvc
default
*.css
default
*.gif
default
*.png
default
*.jpg
default
*.jepg
default
*.js
default
*.html
gateway/index
tomcat配置:
tomcat默认8080端口,有必要的话可以更改du'a端口。此处例子端口仍为8080
先删除tomcat的webapps目录下的软链,如果有软链的话
在server.xml最后,Host标签闭合前,加入如下代码:
nginx配置:
nginx安装过程有点麻烦,可以参考其他博客安装,特别提醒需要“make”命令编译安装
可以参考文章:https://blog.csdn.net/fukai8350/article/details/80634566
https://blog.csdn.net/wowojiani/article/details/80722014
nginx.conf配置:
#user nobody;
worker_processes 1;
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#pid logs/nginx.pid;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
#log_format main '$remote_addr - $remote_user [$time_local] "$request" '
# '$status $body_bytes_sent "$http_referer" '
# '"$http_user_agent" "$http_x_forwarded_for"';
#access_log logs/access.log main;
sendfile on;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
#gzip on;
upstream gateway{ #配置upstream节点,这里节点名为“gateway”
server localhost:8080;
}
server {
listen 80;
server_name localhost;
location / {
proxy_pass http://gateway;
proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
server {
listen 80;
server_name 123456.com;
location / {
proxy_pass http://gateway;
proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
server {
listen 80;
server_name www.123456.com;
location / {
proxy_pass http://gateway;
proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
}
linux里面配置的服务nginx:
#!/bin/sh
# chkconfig: 2345 85 15
# description:Nginx Server
NGINX_HOME=/usr/local/nginx
NGINX_SBIN=$NGINX_HOME/sbin/nginx
NGINX_CONF=$NGINX_HOME/conf/nginx.conf
NGINX_PID=$NGINX_HOME/logs/nginx.pid
NGINX_NAME="Nginx"
. /etc/rc.d/init.d/functions
if [ ! -f $NGINX_SBIN ]
then
echo "$NGINX_NAME startup: $NGINX_SBIN not exists! "
exit
fi
start() {
$NGINX_SBIN -c $NGINX_CONF
ret=$?
if [ $ret -eq 0 ]; then
action $"Starting $NGINX_NAME: " /bin/true
else
action $"Starting $NGINX_NAME: " /bin/false
fi
}
stop() {
kill `cat $NGINX_PID`
ret=$?
if [ $ret -eq 0 ]; then
action $"Stopping $NGINX_NAME: " /bin/true
else
action $"Stopping $NGINX_NAME: " /bin/false
fi
}
restart() {
stop
start
}
check() {
$NGINX_SBIN -c $NGINX_CONF -t
}
reload() {
kill -HUP `cat $NGINX_PID` && echo "reload success!"
}
relog() {
kill -USR1 `cat $NGINX_PID` && echo "relog success!"
}
case "$1" in
start)
start
;;
stop)
stop
;;
restart)
restart
;;
check|chk)
check
;;
status)
status -p $NGINX_PID
;;
reload)
reload
;;
relog)
relog
;;
*)
echo $"Usage: $0 {start|stop|restart|reload|status|check|relog}"
exit 1
esac
备注:eclipse下的server.xml:
1、删除tomcat的server.xml里面配置的Context信息,直接把网站代码放到webapps下面,命名为gateway
2、配置nginx如下:
upstream guanwang{ #配置upstream节点,这里节点名为“guanwang”
server localhost:8080;
}
location /gateway {#gateway
proxy_pass http://guanwang;
proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
location / {#根域名
proxy_pass http://localhost:8080/gateway/gateway/index;
proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
到此结束,官网可以访问了!!!