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

    nginx配置文件详解

    基本配置

    #核心模块配置
    user www; #nginx进程使用的用户
    worker_processes 1; #nginx运行的worker进程数量(建议与CPU数量一致或auto)
    err_log /log/nginx/error.log#错误日志存放目录
    pid        /var/run/nginx.pid;#nginx进程的ip
    
    
    #事件模块配置
    events {
        worker_connections  1024; #一个worker最大的链接数量
      	use epoll;#使用的网络模型为epoll 默认
    }
    
    # http模块配置
    http {
        include       /etc/nginx/mime.types;  #文件后缀与响应数据类型的映射表
        default_type  application/octet-stream; #当后缀找不到映射时使用的默认类型  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"';
    # 指定日志路径 并指定需要使用的格式为main
        access_log  /data/log/nginx/access.log  main;
    
        sendfile        on; # 启用高效文件传输 nginx内部提供的方法
        #tcp_nopush     on;
    
        keepalive_timeout  65; #会话超时时间
    
        #gzip  on;#是否开启压缩功能
    
        include /etc/nginx/conf.d/*.conf; # 包含其他位置的配置文件 (server)
    }
    

    server配置

    一般情况是基本配置放到nginx.conf文件里,把自己服务器的server配置放在一个conf文件里,再再nginx.conf里面include另一个conf文件

    # server配置项位于http之内 之所以分开是为了方便管理
    server {
        listen       80;
        server_name  localhost;
    
        #charset koi8-r;  指定编码方式
        #access_log  /var/log/nginx/host.access.log  main;  #单独指定该服务的日志路径
    
      # 转发路径
        location / {                       # 10.0.0.11  == http://10.0.0.11:80/   /表示跟
            root   /usr/share/nginx/html;  # 访问路径为/时 到/usr/share/nginx/html;下找文件 
           # /将被替换为 root 后的路径
            index  index.html index.htm;   # 默认的主页文件 
          # 该配置表示当访问了地址为10.0.0.11时将返回
                                           # /usr/share/nginx/html/index.html 或/ 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; # 当遇到5xx服务器错误时 返回
        location = /50x.html { #/usr/share/nginx/html/50x.html
            root   /usr/share/nginx/html;     
        }
      
      # 一个server中可以包含多个location配置项
    }
    

    #### 静态文件配置

    静态文件配置可以使用root目录和alias目录,两者的区别在于:

    1)alias指定的目录是准确的,即location匹配访问的path目录下的文件直接是在alias目录下查找的

    2)root指定的目录是location匹配访问的path目录的上一级目录,这个path目录一定要是真实存在root指定目录下的;

    举个栗子,静态资源放置在/data/www/static/目录下


    ##### 1.配置alias目录
    location /static/ {
        alias /data/www/static/;
    }
    

    注意:alias指定的目录后面必须加上"/"

    配置alias后访问http://ip:port/static/dm.jpg时实际访问的是/data/www/static/dm.jpg


    ##### 2.配置root目录
    location /static/ {
        root /data/www/;
    }
    

    注意:location中指定的/static/必须是root指定的/data/www/目录下真实存在的

    两种方式配置的效果是一样的


    配置习惯

    一般情况下,在nginx配置中的良好习惯是:

    1)在location / 中配置root目录

    2)在location /somepath/中配置alias虚拟目录


    参考文章:

    1.https://www.cnblogs.com/bluestorm/p/4574688.html

    2.https://blog.csdn.net/spark_csdn/article/details/80836326

  • 相关阅读:
    Service解析
    Android消息处理
    FragmentTransaction.addToBackStack无效的问题
    Activity生命周期
    width 的100% 与 auto
    parseInt 与 parseFloat 解析
    splice
    <c:forEach>
    笔记,遮罩。。
    nginx 的windows 基本配置
  • 原文地址:https://www.cnblogs.com/zzliu/p/11921769.html
Copyright © 2011-2022 走看看