美文网首页
【Nginx】3、Nginx配置文件介绍

【Nginx】3、Nginx配置文件介绍

作者: 尹小芃槑 | 来源:发表于2019-01-15 10:41 被阅读0次

【Nginx】3、Nginx配置文件介绍

Nginx安装好了之后,配置文件的位置在哪里呢?接下来我们来看一下,并简单介绍一下配置文件中各参数的含义。

Nginx的主要的配置文件是nginx.conf,我们在各个系统上找到这个文件就OK了。

Windows

配置文件是Nginx解压目录的的conf目录中的nginx.conf。

CentOS

使用yum安装nginx的话,配置文件一般会在/etc/nginx

如果找不到的话,可以通过rpm -ql nginx命令来搜索,rpm 是linux的rpm包管理工具,-q 代表询问模式,-l 代表返回列表,这样我们就可以找到nginx的所有安装位置了。

Mac

通过brew安装的话,配置文件一般会在/usr/local/etc/nginx/, 另外可以通过brew info nginx来查看安装目录

找到nginx.conf文件后,我们一起来看看文件中各个参数都有什么作用。

# 运行用户,默认为nobody
# user  nobody;

# Nginx进程,一般设置与CPU核数一样
worker_processes  1;

# 错误日志存放目录, info | notice | warn 表示日志级别
#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

# 进程pid存放日志位置
#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"';
    
    # Nginx访问日志存放位置
    #access_log  logs/access.log  main;

    # 开启高效传输模式 默认为on
    sendfile        on;
    
    # 减少网络报文段的数量
    #tcp_nopush     on;

    # 设置超时时间,也可以说是保持链接的时间
    #keepalive_timeout  0;
    keepalive_timeout  65;
    
    # 开启gzip压缩,一般用于前端页面访问时,减少传输文件的大小,加快页面加载速度
    #gzip  on;

    # 配置一个代理服务
    server {
        # 配置监听端口号
        listen       80;
        
        # 配置域名
        server_name  localhost;
        
        # 配置字符编码
        #charset koi8-r;
        
        # 该服务访问日志存放位置
        #access_log  logs/host.access.log  main;

        location / {
            # 服务默认启动根目录
            root   /usr/local/var/www;
            
            # 默认访问文件
            index  index.html index.htm;
        }

        # 错误页面配置, 404为http状态码,  /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 {
            # 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访问
    # 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;
    #    }
    #}
    
    # 包含的子配置项位置和文件
    include servers/*;
}

Tip

Nginx还有很多很多参数,英文很好的可以直接看官网文档,开源中国提供了Nginx中文文档,w3cschool也提供了中文教程

相关文章

网友评论

      本文标题:【Nginx】3、Nginx配置文件介绍

      本文链接:https://www.haomeiwen.com/subject/uymedqtx.html