win11+Nginx部署前端静态页面/Vue

本文详细介绍了如何使用Nginx部署前端项目,包括下载Nginx稳定版,配置Nginx监听端口和静态文件目录,修改`nginx.conf`文件,启动Nginx服务以及在修改配置后如何重启服务。通过这些步骤,读者可以快速学会将前端打包后的dist文件通过Nginx进行发布。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一、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
### Windows 上 Nginx 实现 Vue 项目开机自启动配置教程 #### 配置概述 为了使 Nginx 和关联的 Vue 应用程序能够在 Windows 启动时自动运行,可以利用 Windows 的服务管理功能来创建一个自定义的服务。这不仅能够简化部署流程,还能提高系统的稳定性和可靠性。 #### 安装 Nginx 并设置为 Windows 服务 安装并配置好 Nginx 是第一步,在此之后需要将其注册成 Windows 服务以便于管理和自动化控制。可以通过 `nssm` 工具轻松完成这项工作[^1]。 ```powershell # 下载 nssm 可执行文件到任意目录, 如 C:\tools\nssm.exe Invoke-WebRequest -Uri "https://nssm.cc/release/nssm-2.24.zip" -OutFile "$env:TEMP\nssm.zip" Expand-Archive $env:TEMP\nssm.zip -DestinationPath C:\tools\ Copy-Item -Path "C:\tools\win64\nssm.exe" -Destination "C:\nginx\nssm.exe" # 使用管理员权限打开 PowerShell 或命令提示符窗口 Set-Location 'C:\nginx' .\nssm install nginx ``` 在弹出的对话框中输入以下参数: - **Application**: `C:\nginx\nginx.exe` - **Startup directory**: `C:\nginx` - **Arguments**: `-g daemon off;` 保存后继续通过命令行确认服务已成功添加: ```powershell Get-Service | Where-Object {$_.Name -like "*nginx*"} Start-Service nginx ``` #### 构建和托管 Vue.js 应用程序 构建生产版本的 Vue.js 应用,并将静态资源放置在一个可由 Nginx 访问的位置下。通常情况下会放在 `/html` 文件夹内。 ```bash cd path/to/vue/project npm run build xcopy /s dist c:\nginx\html ``` 编辑 Nginx 配置文件 (`c:\nginx\conf\nginx.conf`) 来适配前端应用的需求: ```nginx server { listen 80; server_name localhost; location / { root html; index index.html index.htm; try_files $uri $uri/ /index.html; } } ``` #### 设置 Windows 开机启动项 为了让上述操作生效,还需要确保每次重启计算机时都能加载这些更改。除了之前提到过的作为服务的方式外,还可以考虑把启动脚本加入到用户的“启动”文件夹里去实现更简单的方案。 对于个人用户来说,可以直接复制快捷方式至 `%APPDATA%\Microsoft\Windows\Start Menu\Programs\Startup` 路径下的 “启动” 文件夹即可;而对于企业级环境,则建议采用组策略或其他集中化的方法来进行统一管理。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值