nginx.conf配置文件(nginx ubuntu )

介绍

nginx.conf是 核心配置文件

位置:

/etc/nginx/nginx.conf

内容

user www-data;
worker_processes auto;   # 有的不是aotu,是1,值越大并发能力越强,一般是cpu核心的2倍
pid /run/nginx.pid;

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; # 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_disable "msie6";

    # gzip_vary on;   # 是否开启压缩
    # gzip_proxied any;
    # gzip_comp_level 6;   # 值 1-9,值越大,压缩后的体积越小
    # 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;   # 包含该目录下所有 .conf后缀的文件 (重要)
    include /etc/nginx/sites-enabled/*;   # 包含该目录下所有 .conf后缀的文件 (重要)
}


#mail {
#    # See sample authentication script at:
#    # http://wiki.nginx.org/ImapAuthenticateWithApachePhpScript
# 
#    # auth_http localhost/auth.php;
#    # pop3_capabilities "TOP" "USER";
#    # imap_capabilities "IMAP4rev1" "UIDPLUS";
# 
#    server {
#        listen     localhost:110;
#        protocol   pop3;
#        proxy      on;
#    }
# 
#    server {
#        listen     localhost:143;
#        protocol   imap;
#        proxy      on;
#    }
#}

配置文件分为:

  • 全局块
  • event块
  • http块

全局块

从配置文件开始到events块之间的内容,主要包括:

  • user www-data;:配置运行 nginx 服务器的用户、用户组
  • worker_processes auto;:worker_processes表示并发处理量,值越大,处理量也越大。受硬件服务器、操作系统影响
  • pid /run/nginx.pid;:进程pid存放路径和名字

event块

影响nginx与用户的网络连接

  • worker_connections 768;:worker process最大连接数为768。此配置对nginx性能影响较大

http块(重要)

用于配置:

  • 代理、反向代理
  • 缓存
  • 日志保存位置
  • 第三方模块配置

该部分又分为:

  • http全局块
  • server块,可有多个

http全局块

  • 文件引入,如:include /etc/nginx/conf.d/*.conf;
  • MIME-TYPE定义,include /etc/nginx/mime.types;
  • 日志定义,如:access_log /var/log/nginx/access.log;
  • 连接超时时间,如:keepalive_timeout 65;
  • 服务主机配置文件,指定服务器端口号、web路径等,如:include /etc/nginx/sites-enabled/*;

server块

与虚拟主机配置相关,如:将请求转发给 Tomcat

  • server块保存在 /etc/nginx/conf.d/目录下的xxx.conf文件中,这些文件很重要
  • server块保存在 /etc/nginx/sites-enabled/*目录下的文件中,这些文件很重要

/etc/nginx/conf.d/*.conf

浏览该目录:

ls /etc/nginx/conf.d/

在ubuntu安装的nginx中,该目录下没有文件

/etc/nginx/sites-enabled/

浏览该目录:

cd /etc/nginx/sites-enabled/
ls

显示结果如下:

default

默认有一个文件 default

default 内容

查看该文件内容:

vim default

执行结果如下:

##
# You should look at the following URL's in order to grasp a solid understanding
# of Nginx configuration files in order to fully unleash the power of Nginx.
# http://wiki.nginx.org/Pitfalls
# http://wiki.nginx.org/QuickStart
# http://wiki.nginx.org/Configuration
#
# Generally, you will want to move this file somewhere, and start with a clean
# file but keep this around for reference. Or just disable in sites-enabled.
#
# Please see /usr/share/doc/nginx-doc/examples/ for more detailed examples.
##

# Default server configuration
#
server {
    listen 80 default_server;   # 监听所有ipv4的80端口
    listen [::]:80 default_server;   # 监听所有ipv6的80端口

    # SSL configuration
    #
    # listen 443 ssl default_server;
    # listen [::]:443 ssl default_server;
    #
    # Note: You should disable gzip for SSL traffic.
    # See: https://bugs.debian.org/773332
    #
    # Read up on ssl_ciphers to ensure a secure configuration.
    # See: https://bugs.debian.org/765782
    #
    # Self signed certs generated by the ssl-cert package
    # Don't use them in a production server!
    #
    # include snippets/snakeoil.conf;

    root /var/www/html;   # 资源所在的目录位置

    # Add index.php to the list if you are using PHP
    index index.html index.htm index.nginx-debian.html;   # 默认首页

    server_name _;   # 匹配域名。主机绑定多个域名时使用

    location / {   # 通用匹配路径
        # First attempt to serve request as file, then
        # as directory, then fall back to displaying a 404.
        try_files $uri $uri/ =404;   # 查找资源
    }

    # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
    #
    #location ~ \.php$ {
    #    include snippets/fastcgi-php.conf;
    #
    #    # With php7.0-cgi alone:
    #    fastcgi_pass 127.0.0.1:9000;
    #    # With php7.0-fpm:
    #    fastcgi_pass unix:/run/php/php7.0-fpm.sock;
    #}

    # deny access to .htaccess files, if Apache's document root
    # concurs with nginx's one
    #
    #location ~ /\.ht {
    #    deny all;
    #}
}


# Virtual Host configuration for example.com
#
# You can move that to a different file under sites-available/ and symlink that
# to sites-enabled/ to enable it.
#
#server {
#    listen 80;
#    listen [::]:80;
#
#    server_name example.com;
#
#    root /var/www/example.com;
#    index index.html;
#
#    location / {
#        try_files $uri $uri/ =404;
#    }
#}

try_files $uri $uri/ =404; 解释见:
https://www.malaoshi.top/show_1EF5xUBlyV1Z.html


原文出处:/show_1EF5wTmcYyaW.html