3.Nginx服务器基础配置指令

3.1 nginx.conf配置文件

以下为初始的nginx.conf文件

# 全局生效
worker_processes  1;

# 在events部分生效
events {
    worker_connections  1024;
}

# 以下指令在http部分生效
http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;

    # 以下指令在http的server部分生效
    server {
        listen       80;
        server_name  localhost;

        # 以下指令在http/server的location部分生效
        location / {
            root   html;
            index  index.html index.htm;
        }
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }
}# 全局生效
worker_processes  1;

# 在events部分生效
events {
    worker_connections  1024;
}

# 以下指令在http部分生效
http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    #tcp_nopush     on;
    keepalive_timeout  65;

    # 以下指令在http的server部分生效
    server {
        listen       80;
        server_name  localhost;

        # 以下指令在http/server的location部分生效
        location / {
            root   html;
            index  index.html index.htm;
        }
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }
}

全局块

全局快是默认配置文件从开始到events块之间的一部分内容,主要设置一些影响Nginx服务器整体运行的配置指令,作用于Nginx服务器全局。

包括配置运行Nginx服务器的用户(组)、允许生成的worker process数、Nginx进程pid存放路径、日志的存放路径和类型以及配置文件引入。

events块

events块涉及的指令主要影响Nginx服务器与用户的网络连接,这部分的指令对Nginx服务器的性能影响较大,在实际配置中应该灵活调整。

http块

http块是Nginx服务器配置中的重要部分,代理、缓存和日志定义等绝大多数的功能和第三方模型的配置都可以放在这个模块中。

http全局块中配置的指令包含文件引入、MIME-Type定义、日志自定义、是否使用sendfile文件传输、连接超时时间、单连接请求数上限。

server块

server块和“虚拟主机”的概念有密切联系。在使用Nginx服务器提供的web服务时,利用虚拟主机的技术就可以避免为每一个运行的网站提供单独的Nginx服务器,也无需为每一个网站提供独立的进程。虚拟主机技术使得Nginx服务器可以在同一台服务器上运行一组Nginx进程,就可以运行多个网站。

虚拟主机,又称为虚拟服务器、主机空间或网络空间,它是一种技术。虚拟主机的技术主要应用于http、ftp以及email等多项服务将一台服务器的某项或者全不服务内容按逻辑划分为服务多个单位,对外表现为多个服务器。从用户的角度来看,一台虚拟主机和一台独立的硬件服务器是一样的。

 

3.2 常用配置项


#user  nobody;

# 从理论上 work_process 越高,服务器可以支持的并发量就越多
#但实际上会受到来自软件本身的制约、操作系统本身资源和能力、硬件的制约。
worker_processes  1;

# 错误日志的存放路径  默认为 error
# 错误级别 debug info notice warn error crit alert emerg
#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

# 主进程id保存的文件路径
#pid        logs/nginx.pid;


events {
    # 最大连接数,默认值为512,此指令只能在events块中配置
    worker_connections  1024;
}


http {
    # 配置文件的引入
    # 定义MIME-TYPE 可以到 mime.types 文件中配置
    include       mime.types;
    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系统调用在两个文件描述符之间直接传递数据(完全在内核中操作)
    # 从而避免了数据在内核缓冲区和用户缓冲区之间的拷贝,操作效率很高,被称之为零拷贝。
    sendfile        on;

    # sendfile传输的数据量的上限值
    # sendfile_max_chunk size

    #tcp_nopush     on;

    # 连接超时时间 header_timeout 非必填参数,相当于header中的Keep-Alive
    # keepalive_timeout timeout header_timeout  
    #keepalive_timeout  0;
    keepalive_timeout  65;

    # 在客户端和服务器端建立会话后,可以访问的最大次数
    # keepalive_requests 100

    #gzip  on;

    server {
        listen       80;

        # 配置主机对外的名称,可以使用正则表达式,多个使用空格分开
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            root   html;

            #设置默认首页
            index  index.html index.htm;
        }

        # 错误页面设置,如果404,跳转到页面 /html/404.html
        #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 {
            root   html;
        }

        # proxy the PHP scripts to Apache listening on 127.0.0.1:80
        #
        #location ~ \.php$ {
        #    proxy_pass   http://127.0.0.1;
        #}

        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        #location ~ \.php$ {
        #    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;
        #}
    }


    # HTTPS server
    #
    #server {
    #    listen       443;
    #    server_name  localhost;

    #    ssl                  on;
    #    ssl_certificate      cert.pem;
    #    ssl_certificate_key  cert.key;

    #    ssl_session_timeout  5m;

    #    ssl_protocols  SSLv2 SSLv3 TLSv1;
    #    ssl_ciphers  HIGH:!aNULL:!MD5;
    #    ssl_prefer_server_ciphers   on;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}

}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值