首頁 > 軟體

nginx設定多個前端專案實現步驟

2023-03-07 06:01:29

最近一臺伺服器要設定多個前端專案,當然前後端分離就需要nginx來設定了。

單個專案還好說,如下
修改nginx的nginx.conf組態檔

#user  nobody;
worker_processes  1;

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

pid /usr/local/nginx/logs/nginx.pid;


events {
    worker_connections  1024;
}


http {
 
    server {
        listen       8000;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;
        
        location / {
            root   /var/www/;
            #index  index.html index.htm;
        }
        location ~ /static/.*.(gif|jpg|jpeg|png|bmp|swf)$ {
            root /var/www/project;
        }

        location ~ /static/.*.(js|css)$ {
            root /var/www/project;
        }

        location = /project {
            root   /var/www/project;
            index  index.html index.htm;
        }
   
    }

}

但是出現了多個專案也需要在nginx.conf設定

專案基於vue cli 開發的,打包時需要設定一下js,css 等靜態檔案的連線地址
修改如下組態檔

根據專案名字或者路徑名 修改 在對應的專案裡

assetsPublicPath: '/project/'
-----------------------
assetsPublicPath: '/project1/'

然後再來設定nginx.conf

user root;
worker_processes  1;

pid /usr/local/nginx/logs/nginx.pid;

events {
    worker_connections  1024;
}


http {
    include       mime.types;
    default_type  application/octet-stream;

    #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
    #                  '$status $body_bytes_sent "$http_referer" '
    #                  '"$http_user_agent" "$http_x_forwarded_for"';

    #access_log  logs/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    #gzip  on;

    server {
        listen       8000;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;
        
        location / {
            root   /var/www;
            #index  index.html index.htm;
        }

        location = /project1 {
            root   /var/www/project1;
            try_files $uri $uri/ /project1/index.html;
            index  index.html index.htm;
        }
        
        location = /project2{
            root /var/www/project2;
            try_files $uri $uri/ /project2/index.html;
            index  index.html index.htm;
        }

    }

}

此處注意呢 user root; 需要加上, 不然範圍報 500,
然後重啟一下nginx

 先停止
  ./nginx -s quit
 再重啟
 /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf

當然nginx -s reload 可以 ,但是可能報錯, 解決就用上面辦法

成功存取
192.168..:8000/project/index.html
192.168..:8000/project1/index.html

到此這篇關於nginx設定多個前端專案實現步驟的文章就介紹到這了,更多相關nginx設定多前端專案內容請搜尋it145.com以前的文章或繼續瀏覽下面的相關文章希望大家以後多多支援it145.com!


IT145.com E-mail:sddin#qq.com