Nginx配置反向代理-實現前後端完全分離
阿新 • • 發佈:2019-01-26
找到nginx\conf\nginx.conf如下部分:
server {
listen 80;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root html;
index index.html index.htm;
}
修改後如下:
server {
listen 80;
server_name painpointcloud;
#charset koi8-r;
#access_log logs/host.access.log main;
#預設靜態資源
location / {
root html;
index index.html index.htm;
allow all;
}
#其他動態請求反向代理到tomcat容器
location ~ \.(json|do)?$ {
index index;
proxy_pass http://localhost:8080;
}
#配置[企業宣傳]動態請求反向代理到tomcat容器
location ~ \.(com)?$ {
index index;
proxy_pass http://localhost:1010;
}
# 匹配任何以business開始的請求
location ^~ /business/ {
index index;
proxy_pass http://localhost:1010;
}
# 匹配任何以idea開始的請求
location ^~ /idea/ {
index index;
proxy_pass http://localhost:1010;
}
說明:
listen:是監聽的埠,即使用者訪問nginx服務的埠
server_name:服務名,經過測試並不會影響到什麼
location:定義資源型別與伺服器中資源地址url的對映關係,可在/後面定義資源型別,可設定多個location
其中proxy_pass代表要反向代理的伺服器資源url,只要資源型別匹配,在這個url下的子路徑資源都可以訪問到,
其中root代表本地的資源路徑,同樣只要資源型別匹配,這個路徑下的子目錄資源都可以被訪問到,
一個location中只能配置一個root或proxy_pass。
修改後ngnix.conf檔案後,使用nginx -s reload指令,重啟ngnix,如果沒有報錯即重啟成功
C:\Users\admin>F:
F:\>cd F:\develop\server\nginx
F:\develop\server\nginx>nginx -s reload
F:\develop\server\nginx>nginx -s stop
啟動Nginx:start nginx