1沙郭、 nginx介紹
??Nginx在前后端分離框架設(shè)計(jì)中,既可以作為前端的HTTP訪問(wèn)器裳朋,也可以通過(guò)簡(jiǎn)單配置實(shí)現(xiàn)負(fù)載均衡病线,還可以通過(guò)反向代理配置解決前后端分離的JavaScript跨域問(wèn)題。
??解決方案:Nginx服務(wù)器中鲤嫡,監(jiān)聽(tīng)同一個(gè)域名和端口送挑,不同路徑轉(zhuǎn)發(fā)到客戶(hù)端和服務(wù)器,把不同端口和域名的限制通過(guò)反向代理暖眼,來(lái)解決跨域問(wèn)題惕耕。
2、nginx安裝和配置
Ubuntu:apt-get install nginx -y
Centos:yum -y install nginx
2.1 編輯nginx.conf文件
??進(jìn)入/etc/nginx目錄诫肠,編輯nginx.conf文件司澎;將http 域內(nèi)的整個(gè)server{}內(nèi)容清除欺缘,找到include /etc/nginx/conf.d/*.conf;這行,如果沒(méi)有挤安,添加到http {} 的關(guān)閉括號(hào)前一行谚殊,刪除或注釋掉其他的 include *.conf行。
2.2 創(chuàng)建workload_server.conf
??在nginx.conf中已經(jīng)添加:include /etc/nginx/conf.d/*.conf;蛤铜,所以先去/etc/nginx/conf.d目錄嫩絮,清空所有的內(nèi)容,然后在該目錄下創(chuàng)建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/xxx/webapps/demo;
index index.html index.htm;
}
location /demosso{
#配置demosso的IP和端口
proxy_pass http://ip:7080/demosso;
proxy_redirect default;
root /home/xxx/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中配置時(shí)需要輸入ip和端口號(hào)穆刻,而不是使用upstream中的參數(shù)代替置尔;否則無(wú)法跳轉(zhuǎn)到8190;
2.4 nginx操作
2.4.1 啟動(dòng)nginx
nginx
2.4.2 停止nginx
nginx -s stop
2.4.3 查看nginx進(jìn)程
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用戶(hù)
注意:
??demo和demosso中配置時(shí)需要輸入ip和端口號(hào)撰洗,而不是使用upstream中的參數(shù)代替;否則無(wú)法跳轉(zhuǎn)到8190腐芍;
3.1 進(jìn)入到web.xml所在目錄
3.2 配置同nginx一樣的代理端口
3.3 重啟demo
(1)進(jìn)入到/home/demo/bin目錄下(通過(guò)su demo用戶(hù))
(2)關(guān)閉服務(wù)./stop-demo.sh
(3)重啟服務(wù)./start-demo.sh