zoukankan      html  css  js  c++  java
  • nginx配置文件

    nginx都是通过配置文件来进行工作的,多数情况下只需要配置nginx.conf文件,它就能开始工作了。
    nginx的核心配置文件nginx.conf主要由三部分组成。如下图:

    nginx默认配置文件内容如下:

    ##########以下为基本配置###########
    #user  nobody; #用于配置worker进程的运行用户,linux有个默认用户nobody
    worker_processes  1; #配置工作进程数目,根据硬件调整,通常等于CPU数量或者2倍于CPU数量
    
    #error_log  logs/error.log; #配置全局错误日志以及类型,
    #日志级别有:debug|info|notice|warn|error|crit,默认是error级别。
    #error_log  logs/error.log  notice;
    #error_log  logs/error.log  info;
    
    #pid        logs/nginx.pid; #配置进程pid文件
    
    ##############################
    
    ##############以下是事件配置############
    events {
        worker_connections  1024; #配置每个worker进程连接数上限,nginx支持的总连接数=进程数*连接数,#即worker_processes* worker_connections
    }
    #####################################
    
    
    ############以下是http服务配置#########
    
    http {
        ########以下是http服务配置中的基础配置,是通用的
        include       mime.types;#配置nginx支持哪些多媒体类型,可以在conf/mime.types中查看支持哪些。
        default_type  application/octet-stream;#默认支持的文件类型,流格式文件。
    	
        #配置日志的输出格式  $表示nginx自己的变量
        #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日志以及存放路径,并且使用上面main定义的输出日志格式
        #access_log  logs/access.log  main;
    
        sendfile        on;#开启高效文件传输模式
        #tcp_nopush     on;#防止网络阻塞
    
        #keepalive_timeout  0;
        keepalive_timeout  65;#长连接超时时间,单位是秒
    
        #gzip  on; #开启gzip压缩输出,启用后nginx会将服务器返回的较大数据进行压缩后响应到客户端
        #gzip_min_length 1k;#配置最小压缩文件大小
        #gzip_buffers 4 16k;#配置压缩缓冲区
        #gzip_http_version 1.0;#压缩版本(默认1.1)
        #gzip_comp_level 2;压缩等级,数字越大压缩的越厉害
        ########################
        
    	############ 以下是虚拟主机配置,可以配置多个server,每一个server都表示一个独立的虚拟主机站点
        ###配置虚拟主机#############
        server {
            listen       80;#配置的监听端口为80
            server_name  localhost;#配置服务名,也就是提供服务的域名主机名
    
            #charset koi8-r; #配置字符集
    
            #access_log  logs/host.access.log  main;#配置本虚拟机的访问日志
    		#默认的斜杠/的请求,当访问路径中有斜杠/时都会被改location匹配到并进行处理
            location / {
                root   html;#root是配置服务器的默认网站根目录位置,默认为nginx安装目录下的html目录
                index  index.html index.htm;#配置默认的首页文件,多个用空格分开
            }
    
            #error_page  404              /404.html;#配置404页面
    
            # redirect server error pages to the static page /50x.html
            #
            error_page   500 502 503 504  /50x.html;#配置50x错误页面
            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;
            #}
    
            ####以下配置是进行的禁止访问配置,以下是禁止访问.htaccess文件的配置
            # 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;
        #    }
        #}
    
    }
    

    配置完成后可以通过-t命令来进行检测配置是否正确。

    在Windows平台下输入命令:nginx -c -t ./conf/jason.conf,(-c表示配置文件缩写,-t表示test缩写)输出结果如下图所示:

    如果配置有错则会如下提示:

    在linux平台下输入命令: nginx -c ./conf/nginx.conf -t 。其实和Windows平台是一样的。

    查看nginx版本信息:-v和-V,一个小写v,一个大写V,两个的含义有些不同。
    nginx -v:只是显示nginx的当前版本,如下图

    nginx -V:显示nginx版本、编译器版本和配置参数信息,如下图

    以上就是nginx配置文件的大概情况,知道http区块中可以配置多个server服务,每一个server服务都是一个虚拟主机站点,配置完成后可以通过-t来进行检测。

  • 相关阅读:
    我看到的我未曾是你们看到的我。
    nagios状态数据更新不及时问题
    Ubuntu下安装 nagiosgraph报错处理
    禁止、允许PING
    windows批量关机
    [转载]div仿框架(B/S结构软件界面)详解[非quirks模式全兼容]
    rrdtool错误attempt to put segment in horiz list twice
    命令参考大全 iostat
    SAP学习手册
    顾问成长之路
  • 原文地址:https://www.cnblogs.com/jasonboren/p/12907746.html
Copyright © 2011-2022 走看看