百万架构师系列文章阅读体验感更佳

原文链接:https://javaguide.net

公众号:不止极客

Nginx 的初步认识及配置

课程目标

  1. Nginx 在分布式架构中的应用分析
  2. 常用的 Web 服务器及差异
  3. Nginx 的安装以及配置分析
  4. Nginx 虚拟主机配置
  5. 详解 Location 的匹配规则

背景

早期用 F5 做负载均衡

后来通过负载均衡和热备来提高整个的 QPS。

什么是 Nginx

Nginx 是一个高性能的反向代理服务器

  • 正向代理代理的是客户端
  • 反向代理代理的是服务端

Apache、Tomcat、Nginx

​ 都是 HTTP 服务器,他们都会放到我们服务端的服务器上,通过绑定 IP 和 端口的方式,去监听一个端口号。接收客户端的 HTTP 请求。

展示给用户端。

JSP 是动态资源。

apache、nginx 静态文件服务器 css / js
Tomcat 动态文件服务器 jsp / servlet
apache 有一些性能瓶颈的
Nginx 主要是用来处理高并发的请求

Nginx 和 Apache 也是可以做动态解析的,但是它本身不是做这个的。

Nginx 功能

  • 反向代理。
  • 流量分发
  • 动静分离
  • 认证
  • 授权

它还是一个高度模块化的设计

安装 Nginx

http://nginx.org/download/

[root@Darian1 software]# mkdir nginx
[root@Darian1 software]# cd nginx-1.1.4/
[root@Darian1 nginx-1.1.4]# ./configure --prefix=/software/nginx ./configure: error: the HTTP rewrite module requires the PCRE library.
You can either disable the module by using --without-http_rewrite_module
option, or install the PCRE library into the system, or build the PCRE library
statically from the source with nginx by using --with-pcre=<path> option. [root@Darian1 nginx-1.1.4]# yum install pcre-devel
[root@Darian1 nginx-1.1.4]# ./configure --prefix=/software/nginx ./configure: error: the HTTP gzip module requires the zlib library.
You can either disable the module by using --without-http_gzip_module
option, or install the zlib library into the system, or build the zlib library
statically from the source with nginx by using --with-zlib=<path> option. [root@Darian3 nginx-1.4.1]# yum install -y zlib-devel [root@Darian1 nginx-1.1.4]# ./configure --prefix=/software/nginx [root@Darian1 nginx-1.1.4]# make && make install
[root@Darian1 software]# cd nginx [root@Darian1 nginx]# ll
总用量 4
drwxr-xr-x. 2 root root 4096 1月 17 13:58 conf
drwxr-xr-x. 2 root root 68 1月 17 13:58 html
drwxr-xr-x. 2 root root 6 1月 17 13:58 logs
drwxr-xr-x. 2 root root 36 1月 17 13:59 sbin [root@Darian1 nginx]# cd sbin/
[root@Darian1 sbin]# ll
总用量 1704
-rwxr-xr-x. 1 root root 868424 1月 17 13:59 nginx
-rwxr-xr-x. 1 root root 868424 1月 17 13:58 nginx.old
[root@Darian1 sbin]# ./nginx
[root@Darian1 sbin]# ./nginx -s stop
  1. 下载 tar 包
  2. tar -zxvf nginx.tar.gz
  3. ./configure [--prefix]
  4. make && make install

启动和停止

  1. ./nginx
  2. ./nginx -s stop

nginx.conf

[root@Darian1 nginx]# cd conf/
[root@Darian1 conf]# ll
总用量 60
-rw-r--r--. 1 root root 979 1月 17 13:58 fastcgi.conf
-rw-r--r--. 1 root root 979 1月 17 13:59 fastcgi.conf.default
-rw-r--r--. 1 root root 909 1月 17 13:58 fastcgi_params
-rw-r--r--. 1 root root 909 1月 17 13:59 fastcgi_params.default
-rw-r--r--. 1 root root 2837 1月 17 13:59 koi-utf
-rw-r--r--. 1 root root 2223 1月 17 13:59 koi-win
-rw-r--r--. 1 root root 3268 1月 17 13:58 mime.types
-rw-r--r--. 1 root root 3268 1月 17 13:59 mime.types.default
-rw-r--r--. 1 root root 2685 1月 17 13:58 nginx.conf
-rw-r--r--. 1 root root 2685 1月 17 13:59 nginx.conf.default
-rw-r--r--. 1 root root 544 1月 17 13:58 scgi_params
-rw-r--r--. 1 root root 544 1月 17 13:59 scgi_params.default
-rw-r--r--. 1 root root 570 1月 17 13:58 uwsgi_params
-rw-r--r--. 1 root root 570 1月 17 13:59 uwsgi_params.default
-rw-r--r--. 1 root root 3610 1月 17 13:59 win-utf
[root@Darian1 conf]# vim nginx.conf
#user  nobody;
worker_processes 1; #error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info; #pid logs/nginx.pid; events {
# 允许连接的最大数量
worker_connections 1024;
} http {
include mime.types;
default_type application/octet-stream; #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 logs/access.log main; sendfile on;
#tcp_nopush on; #keepalive_timeout 0;
keepalive_timeout 65; #gzip on; server {
listen 80;
server_name localhost; #charset koi8-r; #access_log logs/host.access.log main; location / {
root html;
index index.html index.htm;
} #error_page 404 /404.html; # redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /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;
#} # 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 server
#
#server {
# listen 443;
# server_name localhost; # ssl on;
# ssl_certificate cert.pem;
# ssl_certificate_key cert.key; # ssl_session_timeout 5m; # ssl_protocols SSLv2 SSLv3 TLSv1;
# ssl_ciphers HIGH:!aNULL:!MD5;
# ssl_prefer_server_ciphers on; # location / {
# root html;
# index index.html index.htm;
# }
#} }

Main

event

http

虚拟主机配置

针对不同的端口号做不同的访问,不同域名做不同的访问。

server {
listen 80;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root html;
index index.html index.htm;
}
}

基于ip的虚拟主机

不演示

基于端口号的虚拟主机

server {
listen 8080;
server_name localhost;
location / {
root html;
index index.html;
}
}

我们可以配置一个端口段,去做不同的路由转发。

基于域名的虚拟主机

购买主域名以后,可以基于主域名开放很多二级域名。

www.darian.com / ask.darian.com / git.darian.com / bbs.darian.com

server{
listen 8080;
server_name localhost;
location / {
root html;
index index.html;
}
}
server{
listen 80;
server_name bbs.darian.com;
location / {
root html;
index bbs.html;
}
}
server{
listen 80;
server_name ask.darian.com;
location / {
root html;
index ask.html;
}
}
server{
listen 80;
server_name www.darian.com;
location / {
# 去这个目录下边找
root html;
index index.html;
}
}
[root@Darian1 nginx]# ./sbin/nginx
[root@Darian1 nginx]# ./sbin/nginx -s reload
[root@Darian1 nginx]# ps -ef|grep nginx
root 9595 1 0 15:16 ? 00:00:00 nginx: master process ./sbin/nginx
nobody 9596 9595 0 15:16 ? 00:00:00 nginx: worker process
root 9619 9550 0 15:28 pts/2 00:00:00 grep --color=auto nginx

需要去 html 目录下创建几个文件

[root@Darian1 html]# ll
总用量 24
-rw-r--r--. 1 root root 383 1月 17 13:58 50x.html
-rw-r--r--. 1 root root 26 1月 17 16:13 ask.html
-rw-r--r--. 1 root root 25 1月 17 16:14 bbs.html
-rw-r--r--. 1 root root 151 1月 17 13:58 index.html
-rw-r--r--. 1 root root 7133 1月 17 13:58 ngx_core_module.html

可能需要刷新 DNS

用本地 host 实现了域名的解析。

二级域名随意创造,备案主域名就好了

建议

将配置单独抽出来,放到一个地方去统一地进行管理。

代理解决的事情

  • 负载均衡
  • 缓存
  • 限流
  • 内外网的隔离,做安全性的东西

location

​ nginx 里边,需要知道 location ,对 location 做一些请求的转发。

根据请求的 URI 匹配到对应的 location 以后,去做对应的转发。

配置语法

location [= | ~* | ^~ ] /uri/ {...}

配置规则

location = /uri 精准匹配
location ^~ /uri 前缀匹配
location ~ /uri
location / 通用匹配

规则的优先级

1 location = /
2 location = /index
3 location ^~ /article/
4 location ^~ /article/files/
5 location ~ \.(gif|png|js|css)$
6 location / http://192.168.11.154/ -> 1
http://192.168.11.154/index ->2
http://192.168.11.154/article/files/1.txt ->4
http://192.168.11.154/darian.png ->5

(静态资源服务器,可以做压缩的)

匹配规则是不能重复的。

  1. 精准匹配是优先级最高
  2. 普通匹配(最长的匹配)
  3. 正则匹配

实际使用建议

# 根目录的配置规则,首页是静态页面
location =/ {
}
# 通用匹配 http://192.168.40.128:8080/index.html
location / {
}
# 静态规则,做动静分离
location ~* \.(gif|....)${
}

Nginx模块

​ Nginx 内部都是由一些核心和非核心的第三方模块组成的。它的模块化,我们可以做一些动态的集成和扩展。

​ 除了它本身的功能以外,我们还可以利用第三方的配置去实现更复杂的功能。

默认的:

  • 反向代理
  • email
  • nginx core

模块分类

  1. 核心模块 ngx_http_core_module
  2. 标准模块 http模块
  3. 第三方模块

可以把 模块看成插件

ngx_http_core_module

server{
listen port
server_name ...
localtion{
root ...
}
}

Location 实现 URI 到文件系统路径的映射

  1. error_page

    # redirect server error pages to the static page /50x.html
    #
    error_page 500 502 503 504 /50x.html;
    location = /50x.html {
    root html;
    }

ngx_http_access_module

实现基于 IP 的访问控制功能

  1. allow address | CIDR | unix: | all;
  2. deny address | CIDR | unix: | all;

自上而下检查,一旦匹配,将生效,条件严格的置前

可以用 -t 来看语法有没有问题

[root@Darian1 sbin]# ./nginx -t

如何添加第三方模块

Nginx 不支持动态去安装或者加载模块。安装别的代码的时候需要重新编译。(我们很少去编写模块)

  1. 原来所安装的配置,你必须在重新安装新模块的时候,加上
  2. 不能直接 make install
configure --prefix=/data/program/nginx

安装方法

./configure --prefix=/安装目录 --add-module = /第三方模块的目录
./configure --prefix=/data/program/nginx --with-http_stub_status_module --with-http_random_index_module
cp objs/nginx $nginx_home/sbin/nginx
[root@Darian1 nginx-1.1.4]# cd ../nginx-1.1.4/

[root@Darian1 nginx-1.1.4]# ./configure --prefix=/software/nginx --with-http_stub_status_module --with-http_random_index_module

[root@Darian1 nginx-1.1.4]# ps -ef|grep nginx
root 9595 1 0 15:16 ? 00:00:00 nginx: master process ./sbin/nginx
nobody 9755 9595 0 16:25 ? 00:00:00 nginx: worker process
root 13696 9550 0 19:35 pts/2 00:00:00 grep --color=auto nginx
[root@Darian1 nginx-1.1.4]# kill -9 9595
[root@Darian1 nginx-1.1.4]# kill -9 9755
[root@Darian1 nginx-1.1.4]# ps -ef|grep nginx
root 13699 9550 0 19:35 pts/2 00:00:00 grep --color=auto nginx [root@Darian1 nginx-1.1.4]# cp objs/nginx ../nginx/sbin/nginx
cp:是否覆盖"../nginx/sbin/nginx"? y
[root@Darian1 nginx-1.1.4]#

如果直接 make && make install 会直接就没了。

所以 替换。

[root@Darian1 nginx-1.1.4]# kill -9 13713
[root@Darian1 nginx-1.1.4]# ./configure --prefix=/software/nginx --with-http_stub_status_module --with-http_random_index_module
[root@Darian1 nginx-1.1.4]# make
[root@Darian1 nginx-1.1.4]# cp objs/nginx ../nginx/sbin/nginx
cp:是否覆盖"../nginx/sbin/nginx"? y

http_stub_status_module

location /status{
stub_status on;
}

  • Active connections:当前状态,活动状态的连接数
  • accepts:统计总值,已经接收的客户端请求的总数
  • handled:统计总值,已经处理完成的客户端请求的总数
  • requests:统计总值,客户端发来的总的请求数
  • Reading:当前状态,正在读取客户端请求报文首部的连接的连接数
  • Writing:当前状态,正在向客户端发送响应报文过程中的连接数
  • Waiting:当前状态,正在等待客户端发出请求的空闲连接数

http_random_index_module

www.darian.com

随机显示主页

​ 一般情况下,一个站点默认首页都是定义好的 index.htmlindex.shtml 等等,如果站点下有很多页面想随机展示给用户浏览,那得程序上实现,很麻烦,使用 nginxrandom index 即可简单实现这个功能,凡是以/结尾的请求,都会随机展示当前目录下的文件作为首页

  1. 添加 random_index on 配置,默认是关闭的

    location / {
    root html;
    random_index on;
    index index.html index.htm;
    }
  2. 在 html 目录下创建多个 html 页面

他就会多个 HTML 页面随机地展示在首页。

( 官方的模块,你可以直接通过 with model 去集成,如果是下载第三方插件可以通过 http model 去集成。 )

百万架构师系列文章阅读体验感更佳

原文链接:https://javaguide.net

公众号:不止极客

来源于: https://javaguide.net

微信公众号:不止极客

百万架构师第四十二课:Nginx:Nginx 的初步认识|JavaGuide的更多相关文章

  1. NeHe OpenGL教程 第四十二课:多重视口

    转自[翻译]NeHe OpenGL 教程 前言 声明,此 NeHe OpenGL教程系列文章由51博客yarin翻译(2010-08-19),本博客为转载并稍加整理与修改.对NeHe的OpenGL管线 ...

  2. 第四十二课:基于CSS的动画引擎

    由于低版本浏览器不支持css3 animation,因此我们需要根据浏览器来选择不同的动画引擎.如果浏览器支持css3 animation,那么就使用此动画引擎,如果不支持,就使用javascript ...

  3. 潭州课堂25班:Ph201805201 django 项目 第四十二课 后台 课程相关,用户组管理 (课堂笔记)

    在线课程: 当点击进入页面时,显示所有课程 def get(self, request): courses = Course.objects.select_related('category', 't ...

  4. 第四十二课 KMP算法的应用

    思考: replace图解: 程序完善: DTString.h: #ifndef DTSTRING_H #define DTSTRING_H #include "Object.h" ...

  5. python第四十二课——__str__(self)函数

    4.__str__(self): 作用: 创建完对象,直接打印对象名/引用名我们得到的是对象的内存信息(十六进制的地址信息), 这串数据我们程序员并不关心,我们更希望看到的是属性赋值以后的内容(属性赋 ...

  6. NeHe OpenGL教程 第四十八课:轨迹球

    转自[翻译]NeHe OpenGL 教程 前言 声明,此 NeHe OpenGL教程系列文章由51博客yarin翻译(2010-08-19),本博客为转载并稍加整理与修改.对NeHe的OpenGL管线 ...

  7. centos shell编程6一些工作中实践脚本 nagios监控脚本 自定义zabbix脚本 mysql备份脚本 zabbix错误日志 直接送给bc做计算 gzip innobackupex/Xtrabackup 第四十节课

    centos   shell编程6一些工作中实践脚本   nagios监控脚本 自定义zabbix脚本 mysql备份脚本 zabbix错误日志  直接送给bc做计算  gzip  innobacku ...

  8. NeHe OpenGL教程 第三十二课:拾取游戏

    转自[翻译]NeHe OpenGL 教程 前言 声明,此 NeHe OpenGL教程系列文章由51博客yarin翻译(2010-08-19),本博客为转载并稍加整理与修改.对NeHe的OpenGL管线 ...

  9. NeHe OpenGL教程 第四十五课:顶点缓存

    转自[翻译]NeHe OpenGL 教程 前言 声明,此 NeHe OpenGL教程系列文章由51博客yarin翻译(2010-08-19),本博客为转载并稍加整理与修改.对NeHe的OpenGL管线 ...

  10. NeHe OpenGL教程 第四十六课:全屏反走样

    转自[翻译]NeHe OpenGL 教程 前言 声明,此 NeHe OpenGL教程系列文章由51博客yarin翻译(2010-08-19),本博客为转载并稍加整理与修改.对NeHe的OpenGL管线 ...

随机推荐

  1. Java基础 —— 反射

    动态语言  动态语言,是指程序在运行时可以改变其结构(新函数可以引进,已有的函数可以被删除等结构上的变化).如:JavaScript.Python就属于动态语言,而C.C++则不属于动态语言,从反射的 ...

  2. VTK 正交投影 透视投影

    VTK默认透视投影(近大远小),如果想改成正交投影(平行投影,远近一样): 1.调用vtkCamera的ParallelProjectionOn函数开启 2.通过vtkCamera的SetParall ...

  3. [ARC107D] Number of Multisets题解

    很显然的动态规划. 令 $f_{i,j}$ 为 $n=i$,$k=j$ 时满足题意的集合数. 依题意可得:一个集合可以只由另一个集合添加元素或将所有元素除二得到. 初始:$f_{0,0}=1$. 目标 ...

  4. 为什么推荐在 .NET 中使用 YAML 配置文件

    在现代应用开发中,配置管理是一个非常重要的部分.随着微服务.容器化和云原生架构的流行,使用简单.易读的配置格式变得尤为重要.在 .NET 开发中,虽然 JSON 是默认的配置文件格式,但 YAML(& ...

  5. sed 指定行后或行前插入

    sed 功能非常强大,这里主要列出一些工作中常用到的举例,以后再追加 示例文本 example.cfg Config = { a = 1, b = 1024, c = { ErrLevel = 4, ...

  6. Qt/C++原创推流工具/支持多种流媒体服务/ZLMediaKit/srs/mediamtx等

    一.前言 1.1 功能特点 支持各种本地视频文件和网络视频文件. 支持各种网络视频流,网络摄像头,协议包括rtsp.rtmp.http. 支持将本地摄像头设备推流,可指定分辨率和帧率等. 支持将本地桌 ...

  7. Qt编写地图综合应用17-地址经纬度互转

    一.前言 地址和经纬度互相转换的功能也经常用到,比如上次的路线方案查询的功能,之前官网是提供了直接输入出发地点和目的地的中文汉字,就可以查询到最优的路线,后面只支持输入出发地点和目的地的经纬度坐标了, ...

  8. Sqlsugar 跨库查询小心得(同服务器不同数据库)

    同一个服务器下的不同数据库,目前还没有进行跨服务器的查询,以后有待研究-- 1.使用的是Left Join左查询,因此连接字符串应该是写的第一个表所在的数据库的连接字符串 假设数据库A,B,连接字符串 ...

  9. 在命令中输入信息创建maven项目

    参考链接: 1.使用命令行创建maven web项目 2.Maven 三种archetype说明 3.maven创建项目时在generating project in interactive mode ...

  10. Kubernetes系列(一) - kubernetes入门基本概念

    目录 1. 基本概念 1.1 什么是 Kubernetes 集群 1.2 Kubernetes集群资源组成: 1.3 无状态和有状态的区别 1.3.1 无状态服务 1.3.2 有状态服务 2. Kub ...