跳转至

3. 配置文件

1. 查看配置文件内容

cd /etc/nginx
cat nginx.conf
root@baiduyun:/etc/nginx# cat nginx.conf |grep -Ev '^$|^#'
user www-data;
worker_processes auto;
pid /run/nginx.pid;
include /etc/nginx/modules-enabled/*.conf;
events {
        worker_connections 768;
        # multi_accept on;
}
http {
        ##
        # Basic Settings
        ##
        sendfile on;
        tcp_nopush on;
        tcp_nodelay on;
        keepalive_timeout 65;
        types_hash_max_size 2048;
        # server_tokens off;
        # server_names_hash_bucket_size 64;
        # server_name_in_redirect off;
        include /etc/nginx/mime.types;
        default_type application/octet-stream;
        ##
        # SSL Settings
        ##
        ssl_protocols TLSv1 TLSv1.1 TLSv1.2 TLSv1.3; # Dropping SSLv3, ref: POODLE
        ssl_prefer_server_ciphers on;
        ##
        # Logging Settings
        ##
        access_log /var/log/nginx/access.log;
        error_log /var/log/nginx/error.log;
        ##
        # Gzip Settings
        ##
        gzip on;
        # gzip_vary on;
        # gzip_proxied any;
        # gzip_comp_level 6;
        # gzip_buffers 16 8k;
        # gzip_http_version 1.1;
        # gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript;
        ##
        # Virtual Host Configs
        ##
        include /etc/nginx/conf.d/*.conf;
        include /etc/nginx/sites-enabled/*;   # 我会将此处注释,只保留一个!
}

1. 第一部分:全局指令

# 基本不动
user www-data;              # nginx运行用户
worker_processes auto;      # nginx进程数,一般设置为与cpu核心相同,此处默认即可
pid /run/nginx.pid;         # pid文件
include /etc/nginx/modules-enabled/*.conf;

2. 第二部分:局部指令

# 基本不动
events {
        worker_connections 768;   # 定义每个nginx-worker连接数
        # multi_accept on;
}

3. 第三部分:http{}语句块

# 核心功能配置,包含
# 基本配置,
# server{}虚拟主机配置,
# upstream{}负载均衡池配置等
http {
        ##
        # Basic Settings  # 基本设置内容,基本不用修改
        ##
        sendfile on;
        tcp_nopush on;
        tcp_nodelay on;
        keepalive_timeout 65;     # 超时时间
        types_hash_max_size 2048;
        # server_tokens off;
        # server_names_hash_bucket_size 64;
        # server_name_in_redirect off;
        include /etc/nginx/mime.types;    # 文件扩展名与文件类型扩展表
        default_type application/octet-stream;  # 默认文件类型
        ##
        # SSL Settings  
        ##
        ssl_protocols TLSv1 TLSv1.1 TLSv1.2 TLSv1.3; # Dropping SSLv3, ref: POODLE
        ssl_prefer_server_ciphers on;
        ##
        # Logging Settings
        ##
        access_log /var/log/nginx/access.log; # 访问日志
        error_log /var/log/nginx/error.log;   # 错误日志
        ##
        # Gzip Settings
        ##
        gzip on;  # 静态文件压缩,节省宽带,提高访问速度
        include /etc/nginx/conf.d/*.conf;   # 额外配置内容,一般都会在conf.d中添加额外配置内容!
        include /etc/nginx/sites-enabled/*;
}

4. server{}语句块

# 可以直接写到nginx.conf的http{}中,一般会通过include写到其他配置文件中,便于管理修改!
# server语句块是虚拟主机配置,是最常修改的配置内容
root@baiduyun:/etc/nginx# ls sites-enabled/
default
root@baiduyun:/etc/nginx# cat sites-enabled/default |grep -Ev '^$|^.*#'
server {
        listen 80 default_server;         # 监听的端口
        listen [::]:80 default_server;    
        root /var/www/html;                 # 站点所在路径
        index index.html index.htm index.nginx-debian.html; # 默认主页
        server_name _;                      # 域名,可以有多个,以空格隔开,没有域名使用下划线代替
        location / {                        # location匹配
                try_files $uri $uri/ =404;
        }
}

最后更新: 2022-02-16 06:15:57