1. 程式人生 > 其它 >Ngnix執行vue專案

Ngnix執行vue專案

前後端分離專案,前端專案用ngnix啟動vue頁面專案

1. 下載安裝包

下載地址,解壓後是一個資料夾

2. 部署vue

  1. 找到自己的vue的專案然後輸入命令 npm run build 會在vue目錄下生成一個dist資料夾裡面就是你的vue的專案
  2. 然後開啟這個dist資料夾把裡面的內容複製下來裡面會有兩個檔案一個是index.html是主目錄還有一個是static資料夾
  3. 把他們複製下來然後開啟你的nginx的目錄下的html檔案裡面會有兩個預設檔案直接刪掉不要留,然後把你剛剛複製的檔案貼上進去
  4. 執行起來登入時發現請求頭拿取不到,到conf資料夾的ngin.conf設定 :underscores_in_headers on
  5. 詳情
    `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;
tcp_nopush          on;
tcp_nodelay         on;
types_hash_max_size 2048;
client_max_body_size 20M;

#keepalive_timeout  0;
keepalive_timeout  65;

gzip  on;

upstream localserver {
    #這裡配置伺服器的地址,剛才我們啟動的是8080埠,localserver是我們自定義取的名稱,可以更換成其它的名稱,注意不要和nginx本身的一些名詞衝突就行
server 127.0.0.1:8080;
}


server {
    listen 9999;#這裡是nginx監聽的埠,就是一會兒瀏覽器開啟的埠,訪問的時候用http://testhosts.com:9999,如果這個地方改成80埠,那麼就只需要用http://testhosts.com訪問就行,多個server可以同時監聽80埠,然後代理域名用不同的名字就行
    server_name  testhosts.com;

    #charset koi8-r;

    #access_log  logs/host.access.log  main;
	
root D:\plugins\product_page\dist;#這裡是前端頁面的根路徑
location / {
        
        index  index.html index.htm;
    }
	
location ^~/api/ {#這裡表示瀏覽器所有以/testhosts.com:9999/api開頭的請求,經過nginx代理,到伺服器都用localserver/api/請求,實際localserver就是上面我們配置伺服器地址
  proxy_pass  http://localserver/api/;
    }

}`

3. 雙擊nginx.exe,閃退屬於正常,然後開啟瀏覽器輸入localhost出來如下圖片所示就說明成功了

4. 停止nginx的時候,需要啟動工作管理員結束程序(螢幕下方右鍵調出選單欄),

或者使用命令:

從容停止服務:nginx -s quit

立即停止服務:nginx -s stop

systemctl 停止:systemctl屬於Linux命令
systemctl stop nginx.service

killall 方法殺死程序:killall nginx

`