vue項目通過nginx部署


注:windows和linux皆可

用vue開發的前端項目如何部署到window系統的Nginx服務器上,只需簡單五步

1.在nginx官網下載穩定版本的nginx   地址:http://nginx.org/en/download.html 參考下圖:

                        

 

2.nginx安裝包解壓路徑中不能含有中文,否則nginx啟動會報錯

                         

 

 3.使用npm run build命令,或者其他自己配置的打包命令將vue項目打包,打包完成之后將vue項目下生成的dist目錄拷貝至nginx/html目錄下

                     

 

4.最關鍵的一步,在nginx下的conf\nginx.conf中修改nginx的配置文件,配置修改如下

整理后即為下圖所示 :

                         

注意注意:路由配置為history時,刷新會出現404問題,配置如上圖所示即可解決404問題

 

 

 

 

完整原代碼:

復制代碼
#user  nobody;
worker_processes  1;

error_log logs/error.log;

error_log logs/error.log notice;

error_log logs/error.log info;

pid logs/nginx.pid;

events {
worker_connections 1024;
}

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

#log_format  main  </span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">$remote_addr - $remote_user [$time_local] "$request" </span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">
#                  </span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">$status $body_bytes_sent "$http_referer" </span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">
#                  </span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">"$http_user_agent" "$http_x_forwarded_for"</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">;

#access_log  logs</span>/<span style="color: rgba(0, 0, 0, 1)">access.log  main;

sendfile        on;
#tcp_nopush     on;

#keepalive_timeout  </span><span style="color: rgba(128, 0, 128, 1)">0</span><span style="color: rgba(0, 0, 0, 1)">;
keepalive_timeout  </span><span style="color: rgba(128, 0, 128, 1)">65</span><span style="color: rgba(0, 0, 0, 1)">;

#gzip  on;

server {
      
        
    listen       </span><span style="color: rgba(128, 0, 128, 1)">8200</span><span style="color: rgba(0, 0, 0, 1)">; #前端訪問時需要的端口
    server_name  xxx.xxx.x.x;#前端訪問時需要的ip,默認127.</span><span style="color: rgba(128, 0, 128, 1)">0.0</span><span style="color: rgba(0, 0, 0, 1)">.1或localhost

    #charset koi8</span>-<span style="color: rgba(0, 0, 0, 1)">r;

    #access_log  logs</span>/<span style="color: rgba(0, 0, 0, 1)">host.access.log  main;

    location </span>/<span style="color: rgba(0, 0, 0, 1)"> {
        root   html</span>/<span style="color: rgba(0, 0, 0, 1)">dist; # 打包的文件存放路徑
        index  index.html index.htm;
         #try_files $uri $uri</span>/<span style="color: rgba(0, 0, 0, 1)"> @router; #需要指向下面的@router否則會出現vue的路由在nginx中刷新出現404
         #try_files $uri $uri</span>/ /index.html; ---<span style="color: rgba(0, 0, 0, 1)">解決頁面刷新404問題
    }
    #location </span>^~/<span style="color: rgba(0, 0, 0, 1)">api { 
    #     proxy_set_header Host $host;
    #     proxy_set_header X</span>-Real-<span style="color: rgba(0, 0, 0, 1)">IP $remote_addr; 
    #     proxy_set_header X</span>-Forwarded-<span style="color: rgba(0, 0, 0, 1)">For $proxy_add_x_forwarded_for; 
    #     proxy_buffering off;
    #     rewrite </span>^/api/(.*)$ /$<span style="color: rgba(128, 0, 128, 1)">1</span> <span style="color: rgba(0, 0, 255, 1)">break</span><span style="color: rgba(0, 0, 0, 1)">; 
    #     proxy_pass http:</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">xxxxx:8080; 后端接口地址</span>

#}
#location
/api/ {
#proxy_set_header X
-Real-IP $remote_addr;
#proxy_set_header X
-Forwarded-For $proxy_add_x_forwarded_for;
#proxy_set_header Host $http_host;
#proxy_set_header X
-Nginx-Proxy true;
#proxy_set_header Connection
"";
#proxy_pass http:
//xxxxx:8080; #后端接口地址
#proxy_redirect default ;
#}
location
^~/api {
rewrite
^/api/(.
)$ /$1 break; #重置api
proxy_pass http:
//xxxxx:8080; #
*后端接口地址
}

    #error_page  </span><span style="color: rgba(128, 0, 128, 1)">404</span>              /<span style="color: rgba(128, 0, 128, 1)">404</span><span style="color: rgba(0, 0, 0, 1)">.html;

    # redirect server error pages to the </span><span style="color: rgba(0, 0, 255, 1)">static</span> page /<span style="color: rgba(0, 0, 0, 1)">50x.html
    #
    error_page   </span><span style="color: rgba(128, 0, 128, 1)">500</span> <span style="color: rgba(128, 0, 128, 1)">502</span> <span style="color: rgba(128, 0, 128, 1)">503</span> <span style="color: rgba(128, 0, 128, 1)">504</span>  /<span style="color: rgba(0, 0, 0, 1)">50x.html;
    location </span>= /<span style="color: rgba(0, 0, 0, 1)">50x.html {
        root   html;
    }

    # proxy the PHP scripts to Apache listening on </span><span style="color: rgba(128, 0, 128, 1)">127.0</span>.<span style="color: rgba(128, 0, 128, 1)">0.1</span>:<span style="color: rgba(128, 0, 128, 1)">80</span><span style="color: rgba(0, 0, 0, 1)">
    #
    #location </span>~<span style="color: rgba(0, 0, 0, 1)"> \.php$ {
    #    proxy_pass   http:</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">127.0.0.1;</span>

#}

    # pass the PHP scripts to FastCGI server listening on </span><span style="color: rgba(128, 0, 128, 1)">127.0</span>.<span style="color: rgba(128, 0, 128, 1)">0.1</span>:<span style="color: rgba(128, 0, 128, 1)">9000</span><span style="color: rgba(0, 0, 0, 1)">
    #
    #location </span>~<span style="color: rgba(0, 0, 0, 1)"> \.php$ {
    #    root           html;
    #    fastcgi_pass   </span><span style="color: rgba(128, 0, 128, 1)">127.0</span>.<span style="color: rgba(128, 0, 128, 1)">0.1</span>:<span style="color: rgba(128, 0, 128, 1)">9000</span><span style="color: rgba(0, 0, 0, 1)">;
    #    fastcgi_index  index.php;
    #    fastcgi_param  SCRIPT_FILENAME  </span>/<span style="color: rgba(0, 0, 0, 1)">scripts$fastcgi_script_name;
    #    include        fastcgi_params;
    #}

    # deny access to .htaccess files, </span><span style="color: rgba(0, 0, 255, 1)">if</span> Apache<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">s document root</span>
    # concurs with nginx<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">s one</span>

#
#location
~ /.ht {
# deny all;
#}
}

# another </span><span style="color: rgba(0, 0, 255, 1)">virtual</span> host <span style="color: rgba(0, 0, 255, 1)">using</span> mix of IP-, name-, and port-<span style="color: rgba(0, 0, 0, 1)">based configuration
#
#server {
#    listen       </span><span style="color: rgba(128, 0, 128, 1)">8000</span><span style="color: rgba(0, 0, 0, 1)">;
#    listen       somename:</span><span style="color: rgba(128, 0, 128, 1)">8080</span><span style="color: rgba(0, 0, 0, 1)">;
#    server_name  somename  alias  another.alias;

#    location </span>/<span style="color: rgba(0, 0, 0, 1)"> {
#        root   html;
#        index  index.html index.htm;
#    }
#}


# HTTPS server
#
#server {
#    listen       </span><span style="color: rgba(128, 0, 128, 1)">443</span><span style="color: rgba(0, 0, 0, 1)"> ssl;
#    server_name  localhost;

#    ssl_certificate      cert.pem;
#    ssl_certificate_key  cert.key;

#    ssl_session_cache    shared:SSL:1m;
#    ssl_session_timeout  5m;

#    ssl_ciphers  HIGH:</span>!aNULL:!<span style="color: rgba(0, 0, 0, 1)">MD5;
#    ssl_prefer_server_ciphers  on;

#    location </span>/<span style="color: rgba(0, 0, 0, 1)"> {
#        root   html;
#        index  index.html index.htm;
#    }
#}

}

復制代碼

 

                 

 

5.conf配置文件修改之后,可以在nginx的安裝目錄啟動nginx.exe,也可以使用命令,start nginx啟動項目。

最后通過配置文件中的前端服務器地址和前端端口訪問vue項目即可。

常用命令


免責聲明!

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



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