003.Nginx配置解析
一 Nginx配置文件
1.1 Nginx主配置
- Main位于nginx.conf配置文件的最高层;
- Main层下可以有Event、HTTP层;
- Http层下面允许有多个Server层,用于对不同的网站做不同的配置;
- Server层下面允许有多个Location,用于对不同的路径进行不同模块的配置。
1 user nginx;
2 worker_processes 1;
3
4 error_log /var/log/nginx/error.log warn;
5 pid /var/run/nginx.pid;
1 events {
2 worker_connections 1024;
3 }
1 http {
2 include /etc/nginx/mime.types;
3 default_type application/octet-stream;
4 log_format main '$remote_addr - $remote_user [$time_local] "$request" '
5 '$status $body_bytes_sent "$http_referer" '
6 '"$http_user_agent" "$http_x_forwarded_for"';
7 access_log /var/log/nginx/access.log main;
8 sendfile on;
9 #tcp_nopush on;
10 keepalive_timeout 65;
11 #gzip on;
12 include /etc/nginx/conf.d/*.conf;
13 }
1 server {
2 listen 80;
3 server_name localhost;
4 location / {
5 root /usr/share/nginx/html;
6 index index.html index.htm;
7 }
8 error_page 500 502 503 504 /50x.html;
9 location = /50x.html {
10 root /usr/share/nginx/html;
11 }
12 }
1.2 Nginx全局配置
1 user nginx; #进程用户
2 worker_processes 1; #工作进程,配合和CPU个数保持一致
3 error_log /var/log/nginx/error.log warn; #错误日志路径及级别
4 pid /var/run/nginx.pid; #Nginx服务启动的pid
1.3 Nginx events事件配置
1 events {
2 worker_connections 1024; #每个worker进程支持的最大连接数
3 use epoll; #内核模型,select、poll、epoll
4 }
1.4 Nginx公共配置
1 http {
2 include /etc/nginx/mime.types; #指定在当前文件中包含另一个文件的指令
3 default_type application/octet-stream; #指定默认处理的文件类型可以是二进制
4
5 log_format main '$remote_addr - $remote_user [$time_local] "$request" '
6 '$status $body_bytes_sent "$http_referer" '
7 '"$http_user_agent" "$http_x_forwarded_for"'; #日志格式
8
9 access_log /var/log/nginx/access.log main; #访问日志
10
11 sendfile on; #优化静态资源
12 #tcp_nopush on; #nginx不要缓存数据,而是一段一段发送
13
14 keepalive_timeout 65; #给客户端分配连接超时时间,服务器会在这个时间过后关闭连接。
15
16 #gzip on; #压缩
1.5 Nginx server配置
1 server {
2 listen 80; #监听端口,默认80
3 server_name localhost; #提供服务的域名或主机名
4
5 #charset koi8-r;
6
7 #access_log logs/host.access.log main;
8
9 location / { #控制网站访问路径
10 root /usr/share/nginx/html; #存放网站的路径
11 index index.html index.htm; #默认访问的首页
12 }
13 #error_page 404 /404.html; #错误页面
14
15 # redirect server error pages to the static page /50x.html
16 #
17 error_page 500 502 503 504 /50x.html; #定义请求错误,指定错误代码
18 location = /50x.html { #错误代码重定向到新的location
19 root html;
20 }
21 # another virtual host using mix of IP-, name-, and port-based configuration
22 #
23 #server { #server段配置
24 # listen 8000;
25 # listen somename:8080;
26 # server_name somename alias another.alias;
27
28 # location / {
29 # root html;
30 # index index.html index.htm;
31 # }
32 #}
33
34
35 # HTTPS server
36 #
37 #server { #server段配置
38 # listen 443 ssl;
39 # server_name localhost;
40
41 # ssl_certificate cert.pem;
42 # ssl_certificate_key cert.key; #SSL证书配置
43
44 # ssl_session_cache shared:SSL:1m;
45 # ssl_session_timeout 5m;
46
47 # ssl_ciphers HIGH:!aNULL:!MD5;
48 # ssl_prefer_server_ciphers on;
49
50 # location / {
51 # root html;
52 # index index.html index.htm;
53 # }
54 #}
55 }
二 Nginx网站配置
2.1 Nginx配置网站
1 [root@nginx ~]# vi /etc/nginx/conf.d/base.conf
2 server {
3 server_name base.linuxds.com;
4 location / {
5 root /usr/share/nginx/base;
6 index index.html;
7 }
8 }
9
10 server {
11 server_name blog.linuxds.com;
12 location / {
13 root /usr/share/nginx/blog;
14 index index.html;
15 }
16 location /ok {
17 alias /usr/share/nginx/yes;
18 index index.html;
19 }
20 }
21 [root@nginx01 ~]# mkdir -p /usr/share/nginx/{base,blog,yes}
22 [root@nginx01 ~]# echo '<h1>www</h1>' > /usr/share/nginx/base/index.html
23 [root@nginx01 ~]# echo '<h1>blog</h1>' > /usr/share/nginx/blog/index.html
24 [root@nginx01 ~]# echo '<h1>love</h1>' > /usr/share/nginx/blog/love.html
25 [root@nginx01 ~]# echo '<h1>yes</h1>' > /usr/share/nginx/yes/index.html
26 [root@nginx01 ~]# nginx -t -c /etc/nginx/nginx.conf #检查配置文件
27 [root@nginx01 ~]# nginx -s reload #重载配置文件
2.2 测试访问




2.3 Nginx配置错误页面
1 [root@nginx01 ~]# vi /etc/nginx/conf.d/base.conf
2 server {
3 server_name base.linuxds.com;
4 location / {
5 root /usr/share/nginx/base;
6 index index.html;
7 }
8 }
9
10 server {
11 server_name blog.linuxds.com;
12
13 error_page 404 403 500 502 503 504 /baseerror.html; #配置错误页
14 location /baseerror.html {
15 root /usr/share/nginx/html;
16 }
17
18 location / {
19 root /usr/share/nginx/blog;
20 index index.html;
21 }
22 location /ok {
23 alias /usr/share/nginx/yes;
24 index index.html;
25 }
26 }
27 [root@nginx01 ~]# echo '<h1>Error</h1>' > /usr/share/nginx/html/baseerror.html
28 [root@nginx01 ~]# nginx -t -c /etc/nginx/nginx.conf #检查配置文件
29 [root@nginx01 ~]# nginx -s reload #重载配置文件
2.4 测试Error

三 Nginx相关安全策略
3.1 禁止htaccess
1 location ~/\.ht {
2 deny all;
3 }
3.2 禁止多个目录
1 location ~ ^/(picture|move)/ {
2 deny all;
3 break;
4 }
3.3 禁止/data开头的文件
1 location ~ ^/data {
2 deny all;
3 }
3.4 禁止单个目录
1 location /imxhy/images/ {
2 deny all;
3 }
3.5 特定允许访问
1 root /usr/share/nginx/rewrite/;
2 allow 208.97.167.194;
3 allow 222.33.1.2;
4 allow 231.152.49.4;
5 deny all;
6 auth_basic "xhy";
7 auth_basic_user_file htpasswd;
003.Nginx配置解析的更多相关文章
- 初识nginx——配置解析篇
一.nginx的介绍 nginx是由俄罗斯人开发的一款高性能的http和反向代理服务器,也可以用来作为邮件代理.相比较于其他的服务器,具有占用内存少,稳定性高等优势 二.nginx的配置 nginx的 ...
- Nginx 配置解析
概述:本篇文章主要对Nginx配置文件中一些常用配置进行了讲解,和如何使用Docker进行安装Nginx.因为该文章是回首在工作闲暇之余整理的,还有待完善,如果有疑义和更好的建议的朋友可以留言给我. ...
- Nginx配置解析
#运行用户,默认即是nginx,可不设置 #user nobody; #nginx进程,一般设置为和cpu核数一样 worker_processes 1; #;单个后台worker process进程 ...
- nginx配置解析之客户端真实IP的传递
前后端分离之后,采用nginx作为静态服务器,并通过反向代理的方式实现接口跨域的方式,在降低开发成本的同时也带来了诸多问题,例如客户端真实IP的获取. 在一些特殊场景下,比如风控和支付流程,往往需要获 ...
- NGINX(四)配置解析
前言 nginx配置解析是在初始化ngx_cycle_t数据结构时,首先解析core模块,然后core模块依次解析自己的子模块. 配置解析过程 nginx调用ngx_conf_parse函数进行配置文 ...
- php php-fpm安装 nginx配置php
centos 6.2 linux下安装php5.6.6源码 PHP在 5.3.3 之后已经把php-fpm并入到php的核心代码中了. 所以php-fpm不需要单独的下载安装.要想php支持php-f ...
- Nginx 原理解析和配置摘要
前言 Nginx 作为高性能的 http 服务器,知名度不必多言,相似产品中无出其右.本篇随笔记录我认为较为重要的原理和配置. 1. 原理解析 1.1 结构 以上是 Nginx 的结构图,其包含一个 ...
- [Linux.NET]Nginx 泛解析配置请求映射到多端口实现二级域名访问
由于想实现一个域名放置多个应用运行的目的,而不想通过域名后加端口号方式处理,这种方式处理记起来太麻烦,偷懒党简直不能忍,故而考虑了使用二级域名来处理多个应用同时运行.Google了一番资料并进行了尝试 ...
- Nginx 泛解析配置请求映射到多端口实现二级域名访问
由于想实现一个域名放置多个应用运行的目的,而不想通过域名后加端口号方式处理,这种方式处理记起来太麻烦,偷懒党简直不能忍,故而考虑了使用二级域名来处理多个应用同时运行.Google了一番资料并进行了尝试 ...
随机推荐
- C#版本说明
语言版本 发布时间 .NET Framework要求 Visual Studio版本 C# 1.0 2002.1 .NET Framework 1.0 Visual Studio .NET 2002 ...
- mysql 利用延迟关联优化查询(select * from your_table order by id desc limit 2000000,20)
其实在我们的工作中类似,select * from your_table order by id desc limit 2000000,20会经常遇见,比如在分页中就很常见. 如果我们的sql中出现这 ...
- Spring声明式事务快速上手
1.什么是事务 首先我们要知道什么是事务.知其然,才能知其所以然. 事务(Transaction)是一个业务,是一个不可分割的逻辑工作单元,基于事务可以更好的保证业务的正确性. 这么说可能有点难以理解 ...
- P4017 最大食物链计数(洛谷)
老师开始帮我们查漏补缺啦!我们的老师这两天给了我们一些我们没怎么学的函数和算法,比如STL的函数和拓扑排序之类的,这个题就是讲拓扑排序的. 先看题板: 题目背景 你知道食物链吗?Delia 生物考试的 ...
- 搭建jmeter+influxdb+grafana压测实时监控平台(超详细,小白适用)
1.前言 在使用jmeter做性能测试的时候,监控系统性能的时候,无论是使用插件还是报告生成,都没法实现实时监控.使用JMeter+Influxdb+Grafana可以实现实时监控. 本次环境搭建各软 ...
- Python for循环学习总结笔记
循环是任何语⾔的⼀个必备要素.同样地,for循环就是Python的⼀个重要组成部分.然而还有⼀些内容是初学者常常忽视的.下面是Python for循环学习总结笔记,一起来查漏补缺吧! ...
- IO—》递归
递归的概述 递归,指在当前方法内调用自己的这种现象 public void method(){ System.out.println(“递归的演示”); //在当前方法内调用自己 method(); ...
- docker容器dns之resolv.conf
基础信息 操作系统:CentOS Linux release 7.2.1511 (Core) Docker版本:Server Version: 1.9.1 拉取基础镜像 Rhel:7.2 为直接从do ...
- Mybatis Plus中的lambdaQueryWrapper条件构造图介绍
- Python continue语句
Python continue语句: 当执行到 continue 语句时,将不再执行本次循环中 continue 语句接下来的部分,而是继续下一次循环. lst = [7,8,9,4,5,6] for ...