Windows 安装+开机自启 Nginx+Nginx 配置文件解释_html

最近因为我同学毛都不会为了毕业服务器不敢用 Linux,用的是 Windows,然而用了 Windows 之后并没有想象中的那么顺利,毕竟要装一个 Web 服务器——Nginx。今天我先讲一下 Windows 下怎么安装 Nginx,然后如何配置其在 Windows 下开机自启,最后是 Nginx 配置文件解释。

Windows 安装+开机自启 Nginx+Nginx 配置文件解释_服务器_02


Windows 安装+开机自启 Nginx+Nginx 配置文件解释_服务器_03

Windows 安装 Nginx

在安装之前,先讲一下什么是 Nginx。

Nginx (engine x) 是一个高性能的HTTP和反向代理web服务器,同时也提供了IMAP/POP3/SMTP服务。Nginx是由伊戈尔·赛索耶夫为俄罗斯访问量第二的Rambler.ru站点(俄文:Рамблер)开发的,第一个公开版本0.1.0发布于2004年10月4日。

其将源代码以类BSD许可证的形式发布,因它的稳定性、丰富的功能集、示例配置文件和低系统资源的消耗而闻名。2011年6月1日,nginx 1.0.4发布。

Nginx是一款轻量级的Web 服务器/反向代理服务器及电子邮件(IMAP/POP3)代理服务器,在BSD-like 协议下发行。其特点是占有内存少,并发能力强,事实上nginx的并发能力确实在同类型的网页服务器中表现较好,中国大陆使用nginx网站用户有:百度、京东、新浪、网易、腾讯、淘宝等。

接下来是去下载 Nginx,打开下载页面(http://nginx.org/en/download.html),如图所示。

Windows 安装+开机自启 Nginx+Nginx 配置文件解释_nginx_04

下载最新的稳定版本,这里是 nginx/Windows-1.16.1,点击就可以下载了等待下载完成后就解压缩到某个没有中文等特殊字符啥的路径,如图所示。

Windows 安装+开机自启 Nginx+Nginx 配置文件解释_html_05

双击运行 nginx.exe 就算是运行了 Nginx(可能第一次运行会有防火墙警告弹出,允许就对了),此时打开浏览器,地址栏输入 localhost 回车会出现如图所示的页面。

Windows 安装+开机自启 Nginx+Nginx 配置文件解释_html_06

这样 Nginx 就在 Windows 上安装完成了。

Windows 安装+开机自启 Nginx+Nginx 配置文件解释_服务器_03

Windows 配置 Nginx 开机自启

安装完成后我们要配置 Nginx 开机自启,首先 win+r 打开运行窗口,输入 shell:startup 并回车,弹出一个文件资源管理器窗口,如图所示。

Windows 安装+开机自启 Nginx+Nginx 配置文件解释_nginx_08

只要把需要开机自启的程序或者其快捷方式放进去就配置好开机自启了,如图所示。

Windows 安装+开机自启 Nginx+Nginx 配置文件解释_服务器_09

Windows 安装+开机自启 Nginx+Nginx 配置文件解释_服务器_03

Nginx 配置文件解释

我的 Nginx 配置文件位于 D:\nginx-1.16.1\conf\nginx.conf,和我的差不多就行,不要一模一样。我接下来就解释一下这个配置文件。

#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; # Nginx 进程的 pid


events { # 事件
worker_connections 1024; # 最大连接数
}


http { # 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; # 开启 gzip 压缩输出

server { # 服务器配置
listen 80; # 监听端口
server_name localhost; # 主机名

#charset koi8-r; # 字符及编码

#access_log logs/host.access.log main; # 访问日志

location / { # 根目录位置(我的实际位置是 D:\nginx-1.16.1)
root html; # 再次重新指定根目录位置(我的实际位置:D:\nginx-1.16.1\html)
index index.html index.htm; # 首页文件
}

#error_page 404 /404.html; # 404 错误页面

# redirect server error pages to the static page /50x.html # 静态页面重定向服务器错误页面
#
error_page 500 502 503 504 /50x.html; # 50x 错误页面
location = /50x.html { # 错误页面目录位置
root html;
}

# proxy the PHP scripts to Apache listening on 127.0.0.1:80 # 监听在 127.0.0.1:80 上的 Apache 服务器 PHP 脚本代理
#
#location ~ \.php$ { # PHP 配置文件目录
# proxy_pass http://127.0.0.1; # 代理通行路径
#}

# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000 # 通过监听在 127.0.0.1:9000 的 FastCGI 服务器 PHP 脚本
#
#location ~ \.php$ {
# root html; # 指定根目录
# fastcgi_pass 127.0.0.1:9000; # fastcgi 通行路径
# fastcgi_index index.php; # fastcgi 首页
# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name; # fastcgi 参数
# include fastcgi_params; # 包含的文件
#}

# deny access to .htaccess files, if Apache's document root # 如果是 Apache 文档根路径,拒绝访问 .htaccess 文件
# concurs with nginx's one
#
#location ~ /\.ht { # 位置指定
# deny all; # 拒绝所有
#}
}


# another virtual host using mix of IP-, name-, and port-based configuration # 另一个通过使用混有 IP、名称和基于端口的配置的虚拟主机
#
#server {
# listen 8000; # 监听端口
# listen somename:8080; # 监听的主机及其端口
# server_name somename alias another.alias; # 主机名

# location / { # 根目录位置
# root html;
# index index.html index.htm; # 首页
# }
#}


# HTTPS server # https 服务器
#
#server {
# listen 443 ssl; # 监听端口和协议
# server_name localhost; # 主机名

# ssl_certificate cert.pem; # ssl 协议证书
# ssl_certificate_key cert.key; # ssl 认证密钥

# ssl_session_cache shared:SSL:1m; # ssl 会话缓存
# ssl_session_timeout 5m; # ssl 会话超时时间(这里是 5 分钟)

# ssl_ciphers HIGH:!aNULL:!MD5; # ssl 密码配置
# ssl_prefer_server_ciphers on;

# location / {
# root html;
# index index.html index.htm;
# }
#}

}

实际上这个配置文件可以设置很多东西,我就仅仅讲一下默认的配置。

今天的文章有不懂的可以后台回复“加群”,备注:小陈学Python,不备注可是会被拒绝的哦~!



扫描二维码

获取更多精彩

小陈学Python

Windows 安装+开机自启 Nginx+Nginx 配置文件解释_html_11



Windows 安装+开机自启 Nginx+Nginx 配置文件解释_nginx_12