使用nginx部署vue项目

248 阅读1分钟

1.下载nginx

下载地址:nginx.org/en/download…

2.在vue项目中执行 npm run bulid生成dist文件

3.找到nginx.conf文件的位置( 修改配置)

1.修改端口号

2.修改build生成的dist文件地址

3.修改代理的域名

4.如果访问多个服务,可以有多个serve对应多个服务

`

server {
    listen       8888; # 1.修改端口号
    server_name  localhost;

    #charset koi8-r;

    #access_log  logs/host.access.log  main;

    location / {
        root   C:\Users\86176\Desktop\demo\dist; # 2.修改build生成的dist文件地址
        index  index.html index.htm;
    }
    location /api/ {
        proxy_pass http://192.168.11.11:8080/; # 3.修改代理的域名
    }
    #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;
    #}
}

`

4.nginx.conf文件修改完成后启动nginx

在nginx目录(E:\tools\nginx-1.20.2\nginx-1.20.2)下启动nginx

start nginx //启动nginx

nginx -s reload //重启nginx

nginx -s quit 或者nginx -s stop//关闭nginx

5. 访问localhost:8888(nginx启动窗口可关闭)

6.解决被拒绝访问

1.首先查看防护墙是否关闭,先关闭防火墙再访问

2.如果防护墙关闭:

1.打开控制面板找到程序(程序卸载)

image.png

2.打开启动或关闭windows功能

image.png

3.勾选web管理工具再点击确定

image.png

4.如果端口号被占用也会报这个错(可以修改一个端口号再试试)