Nginx

Nginx

nginx是高性能HTTP服务器/反向代理服务器及电子邮件(IMAP/POP3)代理服务器。它具有并发处理能力强、内存占用少、运行稳定等特点,并支持跨平台运行,如Linux和Windows。

它能够运行静态网页,包括本博客就是用Nginx运行的。

安装地址:

https://nginx.org/en/download.html

解压后双击Nginx.exe启动

html文件夹下放html文件,就可以展示对应界面。

本地预览地址是http://localhost:80/(html文件名)

配置

配置文件在/nginx/conf/nginx.conf

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# 定义运行Nginx进程的用户和用户组,默认nobody(所有用户可运行)
#user nobody;

# 设置工作进程数量(通常等于CPU核心数或auto自动检测)
worker_processes 1;

# 错误日志配置(可设置不同级别:debug|info|notice|warn|error|crit)
#error_log logs/error.log; # 默认错误日志
#error_log logs/error.log notice; # 通知级别日志
#error_log logs/error.log info; # 信息级别日志(最详细)

# 进程ID存储文件路径
#pid logs/nginx.pid;

# 事件处理模块配置
events {
# 单个工作进程的最大并发连接数(总并发= worker_processes * worker_connections)
worker_connections 1024;
}

# HTTP服务器核心配置块
http {
# 引入MIME类型映射文件(定义文件扩展名与Content-Type的对应关系)
include mime.types;

# 默认MIME类型(当无法识别文件类型时使用application/octet-stream)
default_type application/octet-stream;

# 定义日志格式模板(main为格式名称)
#log_format main '$remote_addr - $remote_user [$time_local] "$request" '
# '$status $body_bytes_sent "$http_referer" '
# '"$http_user_agent" "$http_x_forwarded_for"';

# 访问日志设置(使用main格式,路径为logs/access.log)
#access_log logs/access.log main;

# 开启高效文件传输模式(零拷贝技术,提升静态文件传输性能)
sendfile on;

# 在sendfile模式下,将数据包累积到一定大小再发送(提升网络效率)
#tcp_nopush on;

# 连接超时时间设置(0表示禁用长连接)
#keepalive_timeout 0;
keepalive_timeout 65; # 长连接超时时间(秒)

# Gzip压缩开关(开启可减少传输数据量,但增加CPU开销)
#gzip on;

# 虚拟主机配置(一个server块代表一个虚拟主机)
server {
# 监听端口(可指定IP:端口,如listen 127.0.0.1:80)
listen 80;

# 服务器名称(支持域名、通配符、正则匹配,多个用空格分隔)
server_name localhost;

# 字符集设置(默认使用系统字符集,可设置为koi8-r、utf-8等)
#charset koi8-r;

# 本虚拟主机的访问日志(可覆盖http块中的全局设置)
#access_log logs/host.access.log main;

# 位置块配置:匹配所有以/开头的请求
location / {
# 设置请求的根目录(相对路径基于nginx安装目录)
root html;

# 设置默认索引文件(按从左到右优先级查找)
index index.html index.htm;
}

# 自定义错误页面(当出现404错误时返回/404.html)
#error_page 404 /404.html;

# 重定向服务器错误页到静态页面/50x.html
error_page 500 502 503 504 /50x.html;

# 精确匹配路径/50x.html(=表示精确匹配,优先级最高)
location = /50x.html {
root html; # 错误页面所在目录
}

# PHP脚本代理到Apache(示例配置,需要取消注释并修改)
#location ~ \.php$ {
# proxy_pass http://127.0.0.1; # 转发到本机Apache
#}

# PHP脚本通过FastCGI处理(需要PHP-FPM服务运行在9000端口)
#location ~ \.php$ {
# root html;
# fastcgi_pass 127.0.0.1:9000; # PHP-FPM地址
# fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
# include fastcgi_params; # 引入FastCGI参数文件
#}

# 禁止访问.htaccess文件(Apache配置文件,增强安全性)
#location ~ /\.ht {
# deny all; # 拒绝所有访问
#}
}

# 另一个虚拟主机示例(基于IP、域名和端口的混合配置)
#server {
# listen 8000; # 监听8000端口
# listen somename:8080; # 监听8080端口并指定主机名
# server_name somename alias another.alias; # 多域名配置

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

# HTTPS服务器配置示例(需要SSL证书)
#server {
# listen 443 ssl; # 监听443端口并启用SSL
# server_name localhost;

# # SSL证书文件路径(需替换为实际证书)
# ssl_certificate cert.pem;
# ssl_certificate_key cert.key;

# # SSL会话缓存设置(shared表示多个工作进程共享缓存)
# 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