Nginx
是一款是由俄罗斯的程序设计师Igor Sysoev
所开发高性能的 Web和 反向代理 服务器,也是一个 IMAP/POP3/SMTP
代理服务器。去年时间听说Igor Sysoev
被俄罗斯警方带走了,不知道释放。博主是经常使用nginx
的,比如博主的博客网站,公司前后端分离项目等等。
如果服务器是Windows
系统,怎么安装与配置nginx
;博主之前是使用的如下方法,把nginx
安装为windows
服务:
借助Windows Service Wrapper工具,下载该工具后,将其放在 Nginx安装目录下,并重命名为nginx-service.exe
,创建配置文件nginx-service.xml
(名字要和工具名一样),创建nginx-service.exe.config(
为支持NET 4.0 runtime,默认只支持NET 2.0 runtime)
目录如下:
nginx-service.xml
<service>
<id>nginx</id>
<name>Nginx Service</name>
<description>Nginx Service</description>
<logpath>D:\xampp\nginx\logs</logpath>
<log mode="roll-by-size">
<sizeThreshold>10240</sizeThreshold>
<keepFiles>8</keepFiles>
</log>
<executable>D:\xampp\nginx\nginx.exe</executable>
<startarguments>-p D:\xampp\nginx</startarguments>
<stopexecutable>D:\xampp\nginx\nginx.exe</stopexecutable>
<stoparguments>-p D:\xampp\nginx -s stop</stoparguments>
</service>
nginx-service.exe.config
<configuration>
<startup>
<supportedRuntime version="v2.0.50727" />
<supportedRuntime version="v4.0" />
</startup>
<runtime>
<generatePublisherEvidence enabled="false"/>
</runtime>
</configuration>
最后powershell
中运行命令:
nginx-service.exe install
上面的方法一步步还是比较麻烦的,下面还是介绍一下博主个人认为最简单的方法,如果您还没有安装windows包管理器chocolatey,请参考博文【Tool】Windows软件包管理器——chocolatey
choco install nginx
这里有两点需要注意:
choco
安装nginx
,会配套安装NSSM
,这是个好东西,可以把nginx
注册成windows服务
,相当于linux下的Supervisor
和PM2
choco
安装nginx
,在命令行中,最后一次需要您同意选择y[Yes]
之前,注意命令行窗口回写的内容,大致意思:即将执行C:\ProgramData\chocolatey\lib\nginx\tools\chocolateyInstall.ps1
脚本:$toolsDir = Split-Path -parent $MyInvocation.MyCommand.Definition
. "$toolsDir\helpers.ps1"
$pp = Get-PackageParameters
$arguments = @{
packageName = $env:chocolateyPackageName
file = "$toolsDir\nginx-1.17.8.zip"
destination = if ($pp.installLocation) { $pp.installLocation } else { Get-ToolsLocation }
port = if ($pp.Port) { $pp.Port } else { 80 }
serviceName = if ($pp.NoService) { $null } elseif ($pp.serviceName) { $pp.serviceName } else { 'nginx' }
}
if (-not (Assert-TcpPortIsOpen $arguments.port)) {
throw 'Please specify a different port number...'
}
Install-Nginx $arguments
注意看port
那行,没错,80端口
,nginx默认,所以在继续之前,请检查下80端口
是否被占用,一般都是被占用的,毕竟windows服务器的IIS默认就把80端口占了,只要被占用就会安装失败。
那么怎么办?这里算是一点骚操作:您只需要编辑上面的这个脚本,把80
修改为一个未被占用的端口。然后回到刚才没有选择Y的命令窗口,选择Y,继续执行,就能安装成功。
比如博主修改内容:
$toolsDir = Split-Path -parent $MyInvocation.MyCommand.Definition
. "$toolsDir\helpers.ps1"
$pp = Get-PackageParameters
$arguments = @{
packageName = $env:chocolateyPackageName
file = "$toolsDir\nginx-1.17.8.zip"
destination = if ($pp.installLocation) { $pp.installLocation } else { Get-ToolsLocation }
port = if ($pp.Port) { $pp.Port } else { 81 }
serviceName = if ($pp.NoService) { $null } elseif ($pp.serviceName) { $pp.serviceName } else { 'nginx' }
}
if (-not (Assert-TcpPortIsOpen $arguments.port)) {
throw 'Please specify a different port number...'
}
Install-Nginx $arguments
安装好的nginx
在C:\tools
下
nginx
的配置文件也会默认如下面的初始化配置:
#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 81;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root html;
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;
#}
}
}
是不是很简单?
------------------- End -------------------
See Also: