最近做一個跨域前後端分離,但是我並沒有買2個服務器所以就想到了下面的方法
nginx和tomcat等安裝配置就不講了 不懂的可以看下我的文章 centos7建站
我的想法是把後臺放在tomcat上,前端的頁面放在nginx上進行跨域的交互
這裏主要看下nginx的配置
打開nginx.conf
upstream myServer {
server 47.111.166.87:8081;#設置tomcat端口
}
server {
listen 80;
server_name bk.dagoua.com;#後臺域名
location / {
proxy_pass http://myServer;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
server {
listen 80;
server_name blog.dagoua.com;#前臺域名
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root /usr/local/nginx/html/dist;
try_files $uri $uri/ /index.html;
index index.html;
}
#跨域處理
location /api {
proxy_pass http://bk.dagoua.com/;
}
tomcat配置很簡單 原來的端口改爲8081就可以了
<Connector port="8081" protocol="HTTP/1.1"connectionTimeout="20000" redirectPort="8443" />
最後重啓tomcat和nginx就好了