Nginx 部署 Ant Design pro

利用Ant Design pro開發的項目,如何用Nginx部署呢?javascript

第一步:把項目打包,打包命令以下:php

npm run buildcss

運行完畢會在項目目錄下生成dist文件夾。html

第二步:想要測試打包好的代碼是否能夠正常運行,安裝serve,以下命令前端

npm i serve -gjava

serve安裝完畢,利用serve運行打包好的代碼,運行命令:nginx

serve distnpm

測試完畢,代碼能夠正常運行。windows

第三步:用Nginx進行部署。api

1.下載Nginx:http://nginx.org/en/download.html

個人部署在windows系統上,因此下載的windows 版本  nginx-1.16.0,下載完畢解壓,以下圖

 

2.把打包好的dist文件夾複製到html文件夾下。

3.打開conf文件夾,找到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        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 5000;
    # gzip config
    gzip on;
    gzip_min_length 1k;
    gzip_comp_level 9;
    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]\.";

    root /usr/share/nginx/html;

    location / {
        # 用於配合 browserHistory使用
        root html/dist;
        index index.html index.htm;
        try_files $uri $uri/ /index.html;

        # 若是有資源,建議使用 https + http2,配合按需加載能夠得到更好的體驗
        # rewrite ^/(.*)$ https://preview.pro.ant.design/$1 permanent;

    }
    location /api {
        # proxy_pass  http://127.0.0.1:3999;   後臺服務地址
        proxy_pass  http://127.0.0.1:3999;   
        proxy_set_header   X-Forwarded-Proto $scheme;
        proxy_set_header   Host              $http_host;
        proxy_set_header   X-Real-IP         $remote_addr;
    }
}

}

4.在nginx.exe目錄下打開cmd窗口,運行命令:nginx -c conf/nginx.conf   以下圖,nginx服務就開啓成功了,如今能夠在瀏覽器打開地址:http://localhost:5000/,查看咱們部署的網站了

相關文章
相關標籤/搜索