nginx部署 web 项目过程记录
包含nginx 部署说明
、nginx 配置说明
、nginx 注册为 windows 服务操作说明
1、文档基本信息
- 编写人 : duxx
- 编写时间 : 2019年3月1日
- 修改时间 : 暂无修改
- 版本 : V0.1(测试版)
2、服务器环境
win Server2012 64位
3、nginx 版本
nginx-1.14.1
4、web 项目特性
web 项目使用代理访问后台接口
5、实测环境
win10 64位
及 win Server2012 64位
6、搭建过程
1、下载
下载nginx, 选择自己需要的版本,该文档以nginx-1.14.1
为例
2、启动
解压获取到的 nginx 之后将其拷贝至你指定的位置,进入 nginx 根目录,执行启动命令start nginx
,
$ cd D:\nginx-1.14.1
$ start nginx
3、 测试
浏览器访问 http://localhost:80
可以看到nginx的欢迎页面即可
4、总结以上操作常见错误
80 端口已被占用(nginx 默认占用 80 端口): 解决办法:
- 修改 nginx 占用的端口:
打开 nginx 根目录下 conf 文件夹下的 nginx.conf 文件,如下修改 server 下的 listen 节点的值即可
...
http {
....
server {
listen 80;
server_name localhost;
....
}
....
5、修改配置部署项目
我的nginx目录 D:\nginx-1.14.1
,我的部署项目目录 D:\WEB
,部署项目名称 build
, 项目中使用到的后台接口的地址 http://xxx.xx.xx.xx:8090
,webpack.config.js 中代理配置为proxy{ '/lm': { target: 'http://xxx.xx.xx.xx:8090', pathRewrite: { '^/lm': '' }
}
打开 nginx 根目录下 conf 文件夹下的 nginx.conf 文件,如下修改 server 下的 location 节点的值即可
...
http {
...
server {
...
location / {
root D:/WEB/build;
index index.html index.htm;
}
...
}
...
location /lm/ {
proxy_pass http://xxx.xx.xx.xx:8090;
}
...
}
6、 测试
浏览器访问 http://localhost:port
可以看到部署项目首页即可
7、注册 nginx 为服务
现在虽然可以正常使用但是每次开机都需要使用 start nginx
命令启动 nginx 服务,所有我们将其注册为 windows 服务
-
首先 下载winsw-1.18-bin.exe
将其拷贝至 nginx 根目录下,并重命名为nginx-service.exe
-
nginx 根目录下创建配置文件
nginx-service.xml
,内容如下
<service>
<id>nginx</id>
<name>Nginx Service</name>
<description>High Performance Nginx Service</description>
<logpath>D:\nginx-1.14.1\logs</logpath>
<log mode="roll-by-size">
<sizeThreshold>10240</sizeThreshold>
<keepFiles>8</keepFiles>
</log>
<executable>D:\nginx-1.14.1\nginx.exe</executable>
<startarguments>-p D:\nginx-1.14.1</startarguments>
<stopexecutable>D:\nginx-1.14.1\nginx.exe</stopexecutable>
<stoparguments>-p D:\nginx-1.14.1 -s stop</stoparguments>
</service>
- nginx 根目录下创建配置文件
nginx-service.exe.config
,内容如下:
<configuration>
<startup>
<supportedRuntime version="v2.0.50727" />
<supportedRuntime version="v4.0" />
</startup>
<runtime>
<generatePublisherEvidence enabled="false"/>
</runtime>
</configuration>
-
安装 windows 服务
nginx 根目录下
$ cd D:\nginx-1.14.1
$ nginx-service.exe install
此时在任务管理器中可以找到nginx service