美文网首页
Nginx 核心模块与配置实践

Nginx 核心模块与配置实践

作者: 香沙小熊 | 来源:发表于2019-12-29 17:19 被阅读0次

1、Nginx简介:

Nginx是一个高性能的WEB服务器,除它之外Apache、Tomcat、Jetty、IIS,它们都是Web服务器,或者叫做WWW(World Wide Web)服务器,相应地也都具备Web服务器的基本功能。

2.Nginx 基础配置与使用

设置站点

mkdir -p www/kpioneer
echo 'kpioneer is good man' > www/kpioneer/kpioneer.html

nginx.conf 中添加虚拟站点

server {
        listen 80;
        server_name www.kpioneer.com *.kpioneer.com kpioneer.*;
        location / {
          root /usr/www/kpioneer/;
        }
    }
nginx -t

nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful

mac 本机配置IP映射

sudo vi /etc/hosts 
写入
123.57.251.156 www.kpioneer.com
123.57.251.156 good.kpioneer.com
image.png
    server {
        listen 80;
        server_name www.kpioneer.com *.kpioneer.com kpioneer.*;
        root /usr/www/kpioneer/;
        location / {
   #设置默认根目录文件
          index kpioneer.html; 
        }
    }
image.png image.png

静态文件与动态请求配置
在www文件夹中设置static文件目录

mkdir static

nginx.conf

    server {
        listen 80;
        server_name www.kpioneer.com *.kpioneer.com kpioneer.*;
        root /usr/www/kpioneer/;
        location /static {
          alias /usr/www/static/;
        }
        location / {
          index kpioneer.html;
        }
    }
image.png

root与alias主要区别在于nginx如何解释location后面的uri,这会使两者分别以不同的方式将请求映射到服务器文件上。
root的处理结果是:root路径+location路径
alias的处理结果是:使用alias路径替换location路径
alias是一个目录别名的定义,root则是最上层目录的定义。
还有一个重要的区别是alias后面必须要用“/”结束,否则会找不到文件的

root和alias都可以定义在location模块中,都是用来指定请求资源的真实路径,比如:

location /i/ {
  root /data/w3;
}

请求 http://foofish.net/i/top.gif 这个地址时,那么在服务器里面对应的真正的资源是 /data/w3/i/top.gif文件

注意:真实的路径是root指定的值加上location指定的值 。

而 alias 正如其名,alias指定的路径是location的别名,不管location的值怎么写,资源的 真实路径都是 alias 指定的路径 ,比如:

location /i/ {
  alias /data/w3/;
}

同样请求 http://foofish.net/i/top.gif 时,在服务器查找的资源路径是: /data/w3/top.gif

static文件夹目录下

mkdir css
echo 'this is css file' > css/common.css
image.png
        location ~* \.(png|jpg|css|js)$ {
          root /usr/www/static/;
        }
        location = /baidu.html {
          proxy_pass http://www.baidu.com;
        }
image.png
防盗链配置
image.png

mac 本机配置IP映射

sudo vi /etc/hosts 
写入
123.57.251.156 www.kpioneer.com
123.57.251.156 good.kpioneer.com
123.57.251.156 www.bad.com
        location ~* \.(png|jpg|css|js)$ {
          root /usr/www/static/;
         valid_referers none blocked  *.kpioneer.com;
        if ($invalid_referer) {
         return 403;
        }
        }
image.png
防盗链 www.bad.com 的请求获取不到图片
image.png
配置黑名单
cd /usr/local/nginx/conf
echo 'deny 172.16.240.126;' >>ip.black

在nginx.conf进行配置

 include       ip.black;
限速
location / {
            proxy_pass http://localhost:8999/;    #转发到http://localhost:8999
            limit_conn one 10;    #限制并发连接数
            limit_rate 200k;    #限制最高下载速度
            limit_rate_after 1000k;    #下载到指定的文件大小之后开始限速
        }

相关文章

  • Nginx 核心模块与配置实践

    1、Nginx简介: Nginx是一个高性能的WEB服务器,除它之外Apache、Tomcat、Jetty、IIS...

  • Nginx 核心模块与配置实践

    一、Nginx 简介与安装 1、Nginx 简介 Nginx是一个高性能WEB服务器,除它之外Apache、Tom...

  • nginx常用模块介绍

    nginx http功能模块模块说明nginx_http_core_module包括一些核心的http参数配置,对...

  • Nginx核心模块以及指令介绍

    Nginx模块概览 Nginx核心模块以及指令介绍 注意:Nginx的核心模块包含主模块和事件模块,即上图的cor...

  • nginx内核原理

    Nginx的模块 Nginx由内核和模块组成。 Nginx的模块从结构上分为核心模块、基础模块和第三方模块: 核心...

  • HomeWork-1

    一.详细描述常见nginx常用模块和模块的使用示例 1.http_core_module http核心配置模块,该...

  • Nginx配置入门(二):Nginx服务的基本配置

    Nginx服务的基本配置 Nginx在运行时,至少必须加载一个事件类模块和几个核心类模块。这些模块运行时所支持的配...

  • Nginx 配置 HTTPS

    使用Nginx配置HTTPS域名证书 安装SSL模块要在 nginx 中配置 https,就必须安装 ssl 模块...

  • nginx 2

    1 nginx 常用模块整理 nginx 常用模块整理1 http核心模块 ngx_http_core_modu...

  • 第二讲 Nginx模块详解

    本章要点 Nginx 配置文件结构 各个模块的详解 2.1 Nginx配置文件结构 Nginx的配置文件nginx...

网友评论

      本文标题:Nginx 核心模块与配置实践

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