nginx系列4-默认配置语法

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/ytuglt/article/details/85638663

1.默认配置文件基础

[root@localhost ~]# cd /etc/nginx
[root@localhost nginx]# vim nginx.conf 
user  nginx;
worker_processes  1;

error_log  /var/log/nginx/error.log warn;
pid        /var/run/nginx.pid;


events {
    worker_connections  1024;
}


http {
    include       /etc/nginx/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  /var/log/nginx/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    keepalive_timeout  65;

    #gzip  on;

    include /etc/nginx/conf.d/*.conf;
}

这里要关注最后一句 include /etc/nginx/conf.d/*.conf;
nginx启动时首先加载主配置文件,也就是nginx.conf,然后走到配置文件的最后一句,然后加载这句所指定目录的配置文件。

[root@localhost nginx]# cd conf.d/
[root@localhost conf.d]# ls
default.conf

在这个目录下面默认只有一个defaut.conf配置文件。
也就是说在nginx安装完成之后一共包含两个配置文件,一个是主配置文件,一个是Include进来的default.conf文件

2.nginx.conf配置详解

关键字 说明
user 设置nginx服务的系统使用用户
worker_processes 工作进程数
error_log nginx的错误日志
pid nginx服务启动时的pid
events:woker_connections 每个进程允许最大连接数
events:use 工作进程数

user: 跟上一节中–user=nginx有些类似,可以指定用户,但是一般情况下我们就默认是使用nginx即可
worker_processes: 因为nginx是多io复用,进程越多,并发处理能力就越强,一般这个值设置跟cpu核数一致就ok,比如说8核cpu,这个地方我们就设置为8
events:worker_connections: 每个进程对应的最大连接数,这个值在企业应用中是需要进行优化的,对于一般企业来说,设置为1万左右就够用了

在这里插入图片描述

http配置,http包含多个server,可以配置多个站点,servername是配置自己的地址或者域名,location / 指定默认的访问路径,下面的那个location是配置错误页面的路径

3.default.conf文件

server {
    listen       80;
    server_name  localhost;

    #charset koi8-r;
    #access_log  /var/log/nginx/host.access.log  main;

    location / {
        root   /usr/share/nginx/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   /usr/share/nginx/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;
    #location ~ /\.ht {
    #    deny  all;
    #}
}

注意点

修改nginx配置后需要重启nginx服务,重启命令如下:

systemctl restart nginx.service

也可以不关闭服务,直接reload:

systemctl reload nginx.service

猜你喜欢

转载自blog.csdn.net/ytuglt/article/details/85638663
今日推荐