windows本地 安装nginx并部署项目

本文介绍了如何在Windows环境下安装Nginx,并详细解析了如何配置Nginx以部署Vue.js构建的前端项目。通过修改nginx.conf文件,设置server块,将dist目录作为静态文件根目录,确保路由正确重定向,最后启动Nginx服务,实现通过localhost访问项目。

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

windows本地 安装nginx并部署项目

在这里插入图片描述

  • 部署
    • 打包项目,生成 dist
    • 修改 conf 文件夹下的 nginx.conf 文件,主要修改server对象
	
#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       80;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        root   D:/nginxD/nginx-1.20.1/home/dist;

        location / {
            try_files $uri $uri/ @router; # 需要指向下面的@router,否则会出现vue 的路由在ngnix 刷新出现404
            index  index.html index.htm;
        }

        location @router {
            rewrite ^.*$ /index.html last;
        }

        #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;
    #    }
    #}

}

  • 双击 nginx.exe 运行nginx,访问 localhost: 80 即可访问
### 如何在 Windows 上使用 Nginx 部署前端项目 尽管通常较少使用 Windows部署 Nginx 服务,但在特定场景下仍然可行。为了实现这一目标,需遵循一系列配置步骤。 #### 下载安装 Nginx 首先访问[Nginx 官方网站](https://nginx.org/en/download.html),下载适用于 Windows 的版本。解压到指定目录之后,在该目录内找到 `nginx.exe` 文件[^1]。 #### 启动 Nginx 服务 通过命令提示符启动 Nginx 服务。进入存放 `nginx.exe` 的文件夹路径,运行如下指令: ```batch start nginx ``` 这将会启动 Nginx 使其监听默认端口80。此时可以通过浏览器访问 http://localhost/ 测试是否成功显示欢迎页面[^3]。 #### 修改配置文件以支持 Vue.js 应用程序 编辑位于 `conf/nginx.conf` 中的配置文件,确保每一行结尾都加上分号`;` 。针对单页应用(SPA)特性,添加 location 块用于处理历史模式下的路由重定向: ```nginx server { listen 80; server_name localhost; root D:/path/to/vue-app/dist; # 替换成实际打包后的Vue项目的dist目录位置 location / { try_files $uri $uri/ /index.html; } } ``` 上述设置允许任何未匹配资源请求最终指向 index.html ,从而让前端框架接管后续 URL 解析工作。 #### 刷新配置而不重启服务 当调整完毕后无需停止再重新开启整个进程,而是利用以下命令热更新配置: ```batch nginx -s reload ``` 此操作使得新改动即时生效而不会中断现有连接。 #### 访问测试 确认本地开发环境正常运作后,如果希望其他设备也能浏览相同站点,则需要保证这些装置处于同一局域网络之下,替换掉原先地址中的 "localhost" 或者 "127.0.0.1" 成当前机器 IP 地址即可。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值