Nginx服务器的安装与一些基本配置总结

2019-10-17 19:56:50于海丽

- proxy.conf 与Proxy相关的配置文件
- nginx.conf 应用程序的基本配置文件
- sites/
    |- a.conf #允许给每个单独网站建立一个配置文件
    |- b.conf
    |- dir/
        |- c.conf

需要在nginx.conf中使用包含命令

include sites/*.conf;
include sites/*/*.conf;

配置文件结构

http { #嵌入配置文件的根部, 一个http里可以配置多个server

  server { #声明一个站点
    server_name www.website.com; #监听的主机名
    listen 80; #监听套接字所使用的ip地址和端口号

    error_page 404 /not_found.html;
    error_page 500 501 502 503 504 /server_error.html;

    index index.html;

    root /var/www/website/com/html; #定义文档的根目录

    #location, 通过制定的模式与客户端请求的URI相匹配
    location / { #网站的特定位置
    }
    location /admin/ { #网站的特定位置 #
      alias /var/www/locked/; #只能放在 location区段中,为指定路径提供别名
    }

    #操作符,匹配时跟定义顺序无关
    location = /abcd { #精确匹配,不能用正则
    }
    location /abc/ { #url必须以指定模式开始,不能用正则
    }
    location ^~ /abcd$ { #吴标致行为,URI定位必须以指定模式开始,如果匹配,停止搜索其他模式
    }
    location ~ ^/abcd$ { #正则匹配,区分大小写
    }
    location ~* ^/abcd$ { #正则匹配,不区分大小写
    }
    location @test { #定义location区段名,客户端不能访问,内部产生的请求可以,例如try_files或error_page
    }
  }
}


模块
模块化
nginx真正的魅力在于它的模块,整个应用程序建立在一个模块化系统之上,在编译时,可以对每一个模块进行启用或者禁用

index模块
定义往回走哪index页

index index.php index.html /data/website/index.html;

#可以指定多个,但是ngxin提供第一个找到的文件
Log模块
access_log /file/path;
error_log /file/path error;  #level: debug/info/notice/warn/error/crit
日志格式

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/test.log main;
Real IP模块
默认编译nginx不包含这个模块

当通过nginx将用户请求进行转发时,接收请求的应用要拿到用户的真实IP(经转发拿到的是服务器的IP)

real_ip_header X-Forwarded-For;

Access模块
可以禁用ip段

语法

#如果规则之间有冲突,会以最前面匹配的规则为准
deny IP;
deny subnet;
allow IP;
allow subnet;
# block all ips
deny  all;
# allow all ips
allow  all;