Nginx 学习(二).Nginx 基础配置文件详解

Nginx 学习(二).Nginx 基础配置文件详解

1.配置文件总览

##### ==============分界线====================
#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 {
    worker_connections  1024;
}

##### ==============分界线====================
http {
    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        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    #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;
        }

        #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;
        #}
    }


    # another virtual host using mix of IP-, name-, and port-based configuration
    #
    #server {
    #    listen       8000;
    #    listen       somename:8080;
    #    server_name  somename  alias  another.alias;

    #    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;
    #    }
    #}
}
##### ==============分界线====================

接下来是对配置文件中配置项一个个讲解

2.启动用户

默认情况下 worker进程是以nobody用户的身份运行的,如果要指定 ,就需要分开注释,使用user 指令去指定用户;

## 默认配置
###user  nobody;

### 自定义
user  nginx;

2.工作进程 worker process (多进程模型)

配置文件

### 默认配置 worker process 一个线程
worker_processes  1;

### 自定义
#worker_ cpu_ affinity
#worker_cpu_affinity 1000 0100 0010 0001;

2.1 worker_processes

在我们启动nginx 后,会看到两个nginx 进程在运行

[root@localhost ~]# ps -ef |grep nginx
root      24118      1  0 10:40 ?        00:00:00 nginx: master process /package/nginx/sbin/nginx -c /package/nginx/conf/nginx.conf
nobody    24119  24118  0 10:40 ?        00:00:00 nginx: worker process
root      24123  24077  0 10:40 pts/2    00:00:00 grep --color=auto nginx

一个是 master process 另一个 worker process

master process : 只有一个;主要是用来读取配置文件,校验配置文件;reload 时重新加载配置文件,开启 worker process

worker process : 处理请求响应; 在配置文件中可以配置多个

worker processes的值一般设置为CPU核心数;避免worker 抢占CPU, 也可以设置为 worker_ processes auto,自动检测核心并启动相同数量的worker 进程;

以我本机为例

#### 配置 worker_ processes auto 
[root@localhost ~]# ps -ef |grep nginx
root      24118      1  0 10:40 ?        00:00:00 nginx: master process /package/nginx/sbin/nginx -c /package/nginx/conf/nginx.conf
nginx     25653  24118  0 11:10 ?        00:00:00 nginx: worker process
nginx     25654  24118  0 11:10 ?        00:00:00 nginx: worker process
nginx     25655  24118  0 11:10 ?        00:00:00 nginx: worker process
nginx     25656  24118  0 11:10 ?        00:00:00 nginx: worker process
root      25662  24077  0 11:10 pts/2    00:00:00 grep --color=auto nginx

2.2 worker_ cpu_ affinity

CPU 和对应的worker process 绑定; 比如我CPU 有四个 ,每个核心对应的就是 1000,0100,0010,0001 ; 八个核心就是8 位的二进制表示

3.日志文件,PID 文件

##默认配置  不修改就在安装目录 logs 下
#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;
[root@localhost logs]# pwd
/package/nginx/logs
[root@localhost logs]# ls
access.log  error.log  nginx.pid

4. 连接数

每个worker process 最大处理的连接数量

### 默认配置
events {
    worker_connections  1024;
}

5. http协议转发

这里先简单的说一下 下一章节来说一下这个转发协议的具体配置; 一个server 表示一个虚拟主机

http {
    #### mime.types; http里面的头记录文件类型 对应文件后缀
    include       mime.types;
    ###   mime.types; 中没有记录的数据 就流的形式传输
    default_type  application/octet-stream;
    
    ### 零拷贝是否开启
    sendfile        on;
    
    ###保持连接
    keepalive_timeout  65;

    ### 表示一个主机, 可以配置多个主机 相互不影响;
    server {
        ### 监听端口 和 监听主机名/域名
        listen       80;
        server_name  localhost;
 
 		#URL http://localhost:xx/cccc/aaa/bbb
		#URI /cccc/aaa/bbb
		### 匹配URI
        location / {
            ## 匹配上的主目录
            root   html;
            ### 目录下的文件
            index  index.html index.htm;
        }

	    # 异常转发 错误页面
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }
}

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值