Nginx: Difference between revisions
No edit summary |
No edit summary |
||
Line 5: | Line 5: | ||
与 [https://httpd.apache.org/ Apache] 相比, Nginx 具有的优势: | 与 [https://httpd.apache.org/ Apache] 相比, Nginx 具有的优势: | ||
# 轻量 | #轻量 | ||
# 占用内存少 | #占用内存少 | ||
# 配置项更丰富 | #配置项更丰富 | ||
# 高负载下性能更好 | #高负载下性能更好 | ||
=== Installation === | ===Installation=== | ||
官方 repo 配置 请参考 [http://nginx.org/en/linux_packages.html nginx.org] | 官方 repo 配置 请参考 [http://nginx.org/en/linux_packages.html nginx.org] | ||
=== Configuration === | ===Configuration=== | ||
<code>/etc/nginx/nginx.conf</code> 包含全局配置, 任何站点配置请放在 <code>/etc/nginx/conf.d/</code> 目录下. | <code>/etc/nginx/nginx.conf</code> 包含全局配置, 任何站点配置请放在 <code>/etc/nginx/conf.d/</code> 目录下. | ||
=== Performance === | ===Performance=== | ||
# 启用gzip压缩 | #启用gzip压缩 | ||
# | #添加upstream响应时间 | ||
#为静态内容设置适当的过期时间 | |||
#在启用HTTPS的站点总是启用HTTP/2 | |||
# | |||
=== Caching === | ===Caching=== | ||
=== Security === | |||
Nginx 有许多提供Web Application Firewall(WAF)的模块,但是几乎所有模块都需要从源码编译 Nginx. | |||
Naxsi 和 modsecurity是两个流行的选择 | |||
=== 典型配置 === | |||
<code>/etc/nginx/ngxin/conf</code><syntaxhighlight lang="linux-config" line="1"> | |||
user nginx; #Use for CentOS | |||
#user www-data; #Use for Debian | |||
worker_processes auto; #Or set to number of CPU cores | |||
error_log /var/log/nginx/error.log warn; | |||
pid /run/nginx.pid; | |||
include /usr/share/nginx/modules/*.conf; | |||
events { | |||
worker_connections 1024; | |||
} | |||
http { | |||
log_format main '$remote_addr - $remote_user [$time_local] "$request" ' | |||
'$status $body_bytes_sent "$http_referer" ' | |||
'"$http_user_agent" "$http_x_forwarded_for"' | |||
'"$request_time" "$upstream_response_time" $upstream_cache_status'; | |||
access_log /var/log/nginx/access.log main; | |||
sendfile on; | |||
tcp_nopush on; | |||
tcp_nodelay on; | |||
keepalive_timeout 65; | |||
types_hash_max_size 2048; | |||
include /etc/nginx/mime.types; | |||
default_type application/octet-stream; | |||
include /etc/nginx/conf.d/*.conf; | |||
} | |||
</syntaxhighlight><code>/etc/nginx/conf.d/example.conf</code> | |||
<br /> | |||
[[Category:Nginx]] | |||
[[Category:Web]] |
Revision as of 01:13, 13 May 2020
Nginx 已被证明具有极高的稳定性、高性能和高可靠性,并提供了一套强大的工具。
与 Apache 相比, Nginx 具有的优势:
- 轻量
- 占用内存少
- 配置项更丰富
- 高负载下性能更好
Installation
官方 repo 配置 请参考 nginx.org
Configuration
/etc/nginx/nginx.conf
包含全局配置, 任何站点配置请放在 /etc/nginx/conf.d/
目录下.
Performance
- 启用gzip压缩
- 添加upstream响应时间
- 为静态内容设置适当的过期时间
- 在启用HTTPS的站点总是启用HTTP/2
Caching
Security
Nginx 有许多提供Web Application Firewall(WAF)的模块,但是几乎所有模块都需要从源码编译 Nginx.
Naxsi 和 modsecurity是两个流行的选择
典型配置
/etc/nginx/ngxin/conf
user nginx; #Use for CentOS
#user www-data; #Use for Debian
worker_processes auto; #Or set to number of CPU cores
error_log /var/log/nginx/error.log warn;
pid /run/nginx.pid;
include /usr/share/nginx/modules/*.conf;
events {
worker_connections 1024;
}
http {
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"'
'"$request_time" "$upstream_response_time" $upstream_cache_status';
access_log /var/log/nginx/access.log main;
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
types_hash_max_size 2048;
include /etc/nginx/mime.types;
default_type application/octet-stream;
include /etc/nginx/conf.d/*.conf;
}
/etc/nginx/conf.d/example.conf