nginx 部署前端項目(vue)


前提:安裝好nginx

  • 打開nginx目錄,一般是(/usr/local/nginx)

  • npm run build 打好vue包 一般放到(/usr/local/nginx/html/)目錄下

配置:nginx

  • 打開(/usr/local/nginx/conf)目錄

  • 新建一個 xxx.conf 文件,寫入:

server {
        listen       80;    //映射出的端口
        server_name  abc.com; //訪問的域名或ip(默認是localhost,有時會被占用,是不是隨便輸個域名都可以?待驗證)
        location / {
	    root   /usr/local/nginx/html/prod_dist; //放代碼的路徑(index.html的根目錄)
            index  index.html index.htm;
        }

        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
}

  • 還是此目錄,打開nginx.conf,找對位置(在http內的標記一和標記二)
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;

//標記二:開啟壓縮靜態資源
    gzip  on;
    gzip_min_length 1k;
    gzip_buffers 4 16k;
    gzip_comp_level 2;
    gzip_types text/plain application/javascript application/x-javascript text/css application/xml text/javascript application/x-httpd-php image/jpeg image/gif image/png;
    gzip_vary on;
    gzip_disable "MSIE [1-6]\.";

    

    include api.conf;  
    include  xxx.conf;  //標記一,引入server配置

重啟nginx生效配置

進入 /usr/local/nginx/sbin
命令行:./nginx -s reload

完成:訪問abc.com

參考網站

https://blog.csdn.net/qq_35620501/article/details/89408581 資源壓縮


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM