Nginx

From The Power of Many
Revision as of 01:13, 13 May 2020 by Snowshi (talk | contribs)

Nginx

Nginx 已被证明具有极高的稳定性、高性能和高可靠性,并提供了一套强大的工具。

Apache 相比, Nginx 具有的优势:

  1. 轻量
  2. 占用内存少
  3. 配置项更丰富
  4. 高负载下性能更好

Installation

官方 repo 配置 请参考 nginx.org

Configuration

/etc/nginx/nginx.conf 包含全局配置, 任何站点配置请放在 /etc/nginx/conf.d/ 目录下.

Performance

  1. 启用gzip压缩
  2. 添加upstream响应时间
  3. 为静态内容设置适当的过期时间
  4. 在启用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