Nginx服务器基础配置详解

来源:互联网 发布:渐飞数据库 编辑:程序博客网 时间:2024/06/04 19:55

Nginx配置整体认识

默认情况下,Nginx服务器配置文件都存放在安装目录Conf中,主配置文件名为nginx.conf 。其内容如下:

#user  nobody;                            #全局块worker_processes  1;#error_log  logs/error.log;#error_log  logs/error.log  notice;#error_log  logs/error.log  info;#pid        logs/nginx.pid;events {                                   #events 块    worker_connections  1024;}http {                                      #http块    include       mime.types;               #http全局块    default_type  application/octet-stream;    #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '    #                  '$status $body_bytes_sent "$http_referer" '    #                  '"$http_user_agent" "$http_x_forwarded_for"';    #access_log  logs/access.log  main;    sendfile        on;    #tcp_nopush     on;    #keepalive_timeout  0;    keepalive_timeout  65;    #gzip  on;    server {                                #server块        listen       80;                    #server全局块        server_name  localhost;        #charset koi8-r;        #access_log  logs/host.access.log  main;        location / {                       #location块            root   html;            index  index.html index.htm;        }        #error_page  404              /404.html;        # redirect server error pages to the static page /50x.html        #        error_page   500 502 503 504  /50x.html;        location = /50x.html {           #location块            root   html;        }        # proxy the PHP scripts to Apache listening on 127.0.0.1:80        #        #location ~ \.php$ {             #location块        #    proxy_pass   http://127.0.0.1;        #}        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000        #        #location ~ \.php$ {           #location块        #    root           html;        #    fastcgi_pass   127.0.0.1:9000;        #    fastcgi_index  index.php;        #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;        #    include        fastcgi_params;        #}        # deny access to .htaccess files, if Apache's document root        # concurs with nginx's one        #        #location ~ /\.ht {        #    deny  all;        #}    }    # another virtual host using mix of IP-, name-, and port-based configuration    #    #server {                              #server块    #    listen       8000;    #    listen       somename:8080;    #    server_name  somename  alias  another.alias;    #    location / {                     #location块    #        root   html;    #        index  index.html index.htm;    #    }    #}    # HTTPS server    #    #server {    #    listen       443 ssl;    #    server_name  localhost;    #    ssl_certificate      cert.pem;    #    ssl_certificate_key  cert.key;    #    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;    #    }    #}}

从配置文件我们可以总结如下
这里写图片描述
nginix.conf配置一共有三块组成:全局块、events块和http块。在http块中,包含多个server块,每个server块中又可以包含多个location块。

配置文件各模块意义

全局块

全局块是默认配置文件从开始到events模块之间的一部分内容。主要设置一下影响Nginx服务器整体运行的配置指令,该指令作用域是Nginix服务器全局。
通常包括运行Nginx服务器用户(组)、允许生产的worker process数、Nginix进程Pid存放路径、日志存放路径和类型以及配置文件引入等。

events块

events块涉及的指令主要影响Nginx服务器与用户的网络连接。常用配置有:
是否开启对多worker process下的网络连接进行序列化;
是否允许同时接受多个网络连接;
选择哪种事件模型处理连接请求;
每个worker process 可以同时支持的最大连接数等。
这一部分指令对Nginix服务器的性能影响较大,在实际配置中,应该根据实际情况,灵活调整。

http块

http块是Nginix服务器配置中重要部分。代理、缓存和日志定义绝大部分功能和第三方模块的配置都可以放到这个模块。http全局块中主要配置如下:
文件引入;
MIME-TYPE定义;
日志自定义;
是否使用sendfile传输文件;
连接超时时间;
单连接请求数上线等

server块

server块和“虚拟主机”概念有着密切联系。每个server块可以相当是一台虚拟主机。server块的作用域为本server块,不会影响到其他的server块。和http块相同,server块也可以包含自己的全局块,同时还可以包含多个location块。server全局块主要的两个配置如下:
虚拟主机的监听配置;
虚拟主机的名称或者ip配置

location块

每个server块可以包含多个location块,从严格意思来说,location其实是server是server块的一个指令。主要作用是,基与Nginx服务器接受到的请求字符串,对除虚拟主机名之外的字符串进行匹配,对特定的请求进行处理。地址定向、数据缓存和应答控制等功能都在这部分实现。

0 0