1、 nginx介绍
Nginx在前后端分离框架设计中,既可以作为前端的HTTP访问器,也可以通过简单配置实现负载均衡,还可以通过反向代理配置解决前后端分离的JavaScript跨域问题。
解决方案:Nginx服务器中,监听同一个域名和端口,不同路径转发到客户端和服务器,把不同端口和域名的限制通过反向代理,来解决跨域问题。
2、nginx安装和配置
Ubuntu:apt-get install nginx -y
Centos:yum -y install nginx
2.1 编辑nginx.conf文件
进入/etc/nginx目录,编辑nginx.conf文件;将http 域内的整个server{}内容清除,找到include /etc/nginx/conf.d/*.conf;这行,如果没有,添加到http {} 的关闭括号前一行,删除或注释掉其他的 include *.conf行。
2.2 创建workload_server.conf
在nginx.conf中已经添加:include /etc/nginx/conf.d/*.conf;,所以先去/etc/nginx/conf.d目录,清空所有的内容,然后在该目录下创建workload_server.conf文件;
2.3 部署反向代理
1)配置web主入口;
2)配置cas;
server {
listen 8190 default_server;
listen [::]:8190 default_server;
server_name _;
location / {
root /var/www/html/list;
# root /usr/share/nginx/html;
index index.html index.htm index.nginx-debian.html;
if ($request_method = 'OPTIONS') {
add_header 'Access-Control-Allow-Origin' '*';
add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
add_header 'Access-Control-Allow-Headers' 'DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Content-Range,Range';
add_header 'Access-Control-Max-Age' 1728000;
add_header 'Content-Type' 'text/plain; charset=utf-8';
add_header 'Content-Length' 0;
return 204;
}
if ($request_method = 'POST') {
add_header 'Access-Control-Allow-Origin' '*';
add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
add_header 'Access-Control-Allow-Headers' 'DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Content-Range,Range';
add_header 'Access-Control-Expose-Headers' 'DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Content-Range,Range';
}
if ($request_method = 'GET') {
add_header 'Access-Control-Allow-Origin' '*';
add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
add_header 'Access-Control-Allow-Headers' 'DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Content-Range,Range';
add_header 'Access-Control-Expose-Headers' 'DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Content-Range,Range';
}
# try_files $uri $uri/ =404;
}
location /demo {
#配置demo的IP和端口
proxy_pass http://ip:7080/demo;
add_header 'Access-Control-Allow-Origin' '*';
add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
add_header 'Access-Control-Allow-Headers' 'DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Content-Range,Range';
add_header 'Access-Control-Expose-Headers' 'DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Content-Range,Range';
proxy_set_header Host $host:$server_port;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Cookie $http_cookie;
proxy_redirect default;
#root /home/bdoc/webapps/demo;
index index.html index.htm;
}
location /demosso{
#配置demosso的IP和端口
proxy_pass http://ip:7080/demosso;
proxy_redirect default;
root /home/bdoc/webapps/demosso;
proxy_read_timeout 600s;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header Host $host:$server_port;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
index index.html index.htm;
}
}
注意
demo和demosso中配置时需要输入ip和端口号,而不是使用upstream中的参数代替;否则无法跳转到8190;
2.4 nginx操作
2.4.1 启动nginx
nginx
2.4.2 停止nginx
nginx -s stop
2.4.3 查看nginx进程
ps -ef | grep nginx
2.4.4 修改nginx.conf后重启nginx
nginx -s reload
/usr/local/nginx/sbin/nginx -s reload
3、修改demo的web.xml文件
su demo用户
注意:
demo和demosso中配置时需要输入ip和端口号,而不是使用upstream中的参数代替;否则无法跳转到8190;
3.1 进入到web.xml所在目录
3.2 配置同nginx一样的代理端口
3.3 重启demo
(1)进入到/home/demo/bin目录下(通过su demo用户)
(2)关闭服务./stop-demo.sh
(3)重启服务./start-demo.sh