一、Nginx发布前端项目

1、先将前端build到dist文件夹/将含有index.html的静态文件放置到dist文件夹

2、下载Nginx
Nginx官网下载地址

可下载 Stable version稳定版,点击: nginx/Windows-1.22.1

                                    Stable version
CHANGES-1.22	nginx-1.22.1   pgp	nginx/Windows-1.22.1   pgp

3、将下载的压缩包放置在自定义目录并解压(目录不要含有中文)
4、进入nginx目录,找到nginx.exe,不要双击打开!不要双击打开!不要双击打开!

如果打开了可通过Ctrl+Shift+ESC打开控制台管理器关闭【nginx.exe(32位)】

5、找到\conf\nginx.conf打开,找到如下代码并修改

【仅用修改listen端口,和root 的dist目录】
root 后的目录不可以加双引号"",不可以用反斜杠\

server {
        listen       8082;#修改默认端口
        server_name  localhost;#可不做修改,默认用本机ip
        location / {
            root   D:/Users/W/nginx/html/dist;#修改默认的dist文件目录
            index  index.html index.htm;#可不做修改,默认启用文件为index.html
        }

完整版如下:


#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       8082;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            root   D:/Users/W/nginx/html/dist;
            index  index.html index.htm;
        }

        #error_page  404              /404.html;

        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }

        # proxy the PHP scripts to Apache listening on 127.0.0.1:80
        #
        #location ~ \.php$ {
        #    proxy_pass   http://127.0.0.1;
        #}

        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        #location ~ \.php$ {
        #    root           html;
        #    fastcgi_pass   127.0.0.1:9000;
        #    fastcgi_index  index.php;
        #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
        #    include        fastcgi_params;
        #}

        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        #location ~ /\.ht {
        #    deny  all;
        #}
    }


    # another virtual host using mix of IP-, name-, and port-based configuration
    #
    #server {
    #    listen       8000;
    #    listen       somename:8080;
    #    server_name  somename  alias  another.alias;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}


    # HTTPS server
    #
    #server {
    #    listen       443 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:!aNULL:!MD5;
    #    ssl_prefer_server_ciphers  on;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}

}

6、启动nginx,找到nginx.exe,在该目录cmd

start nginx

7、访问地址,如不显示可多刷新几次

http://localhost:8082/

二、修改配置文件后重启

1、已经启动nginx后再次修改nginx.conf配置文件,修改结束后重启nginx

nginx -s reload
Logo

前往低代码交流专区

更多推荐