前後端nginx部署(反向代理解決跨域問題)

最近寫了一個簡單的博客系統,包括客戶端,管理端,服務端。客戶端和管理端是純粹的前端項目,用Nodejs開發的服務端。

需要解決的問題:三個項目分開部署,他們之間的通信的問題!

問題的出現原因就是跨域訪問

Nginx可以爲客戶端和管理端配置反向代理,即可解決跨域問題,當然還有其他方式。

下面貼上nginx配置文件

    server {
         listen 4001;
         server_name localhost;
         default_type 'text/html';
         charset utf-8;
         location / {
             root C:/Users/huanyu.xu/Desktop/workspace/rainy/admin;
             index layout.html index.html index.htm;
         }

         location /api/ {
            proxy_pass http://localhost:3000/;
         }

         error_page 404 /404.html;
    }
    server {
         listen 4002;
         server_name localhost;
         default_type 'text/html';
         charset utf-8;
         location / {
             root C:/Users/huanyu.xu/Desktop/workspace/rainy/client;
             index layout.html index.html index.htm;
         }
         location /api/ {
            proxy_pass http://localhost:3000/;
         }
         error_page 404 /404.html;
    }

以上爲客戶端和管理端Nginx配置(靜態網站)

服務端爲nodejs項目,使用pm2 守護node服務進程即可


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章