Py学习  »  NGINX

nginx学习从入门到精通

程序员小哲 • 3 年前 • 338 次点击  

为什么使用Nignx

  • Nginx是一个高性能的HTTP和反向代理服务器
  • 内存消耗小:开启10个nginx才占150M内存 ,nginx处理静态文件好,耗费内存少
  • 跨平台、配置简单,非阻塞、高并发连接:处理2-3万并发连接数,官方监测能支持5万并发

Nginx基本用法

开启nginx:sudo service nginx start
重启nginx:sudo service nginx reload
关闭nginx:nginx -s stop

Nginx的主配置文件

我们一般把nginx.conf放在/etc/nginx/nginx.conf

user  nginx;
# worker_processes的数值越大,Nginx的并发能力就越强
worker_processes  1;

# error_log代表Nginx错误日志存放的位置
error_log  /var/log/nginx/error.log warn;
# pid是Nginx运行的一个标识
pid        /var/run/nginx.pid;

events {
	# worker_connections的数值越大,Nginx的并发能力就越强
    worker_connections  1024;
}

http {
	# include代表引入一个外部文件
	# mime.types中存放着大量媒体类型
    include       /etc/nginx/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  /var/log/nginx/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    keepalive_timeout  65;

    #gzip  on;
    
	#引入了conf.d下以.conf为结尾的配置文件
    include /etc/nginx/conf.d/*.conf;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36

Nginx的其他文件

其他配置文件放在/etc/nginx/conf.d/*.conf
基本配置信息(这样是访问静态资源的)

nginx ssl简单配置(https认证)-- 点我查看

server {

	# listen代表Nginx监听ipv4的端口号
    listen       80;
    # listen代表Nginx监听ipv6的端口号
    listen  [::]:80;
    # server_name代表Nginx接受请求的IP
    server_name  localhost;

    location / {
    	# root:将接受到的请求根据/usr/share/nginx/html去查找静态资源
        root   /usr/share/nginx/html;
        # index:默认去上述的路径中找到index.html或index.htm
        index  index.html index.htm;
    }

	#错误跳转页面(没有不耽误)
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;
    }
    
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23

Nginx反向代理

学习反向代理时,应该先了解正向代理

正向代理:

1.正向代理服务是由客户端设立的
2.客户端了解代理服务器和目标服务器都是谁
3.帮助咱们实现突破访问权限,提高访问的速度,对目标服务器隐藏客户端的ip地址

在这里插入图片描述
反向代理

1.反向代理服务器是配置在服务端的
2.客户端不知道访问的到底是哪一台服务器
3.达到负载均衡,并且可以隐藏服务器真正的ip地址

在这里插入图片描述
反向代理代码实现:

修改/etc/nginx/conf.d/*.conf
这里我们访问我们的tomcat服务器

server {
    listen       80;
    listen  [::]:80;
    server_name  localhost;

    location / {
    	# 反向代理的服务器:127.0.0.1可以换成域名或者ip
        proxy_pass http://127.0.0.1:8080/;
    }
    
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

Nginx的location路径映射

1、 = 匹配

location = / {
	#精准匹配,主机名后面不能带能和字符串
}
  • 1
  • 2
  • 3
  • 1
  • 2
  • 3

2、 通用匹配

location /xxx {
	#匹配所有以/xxx开头的路径
}
  • 1
  • 2
  • 3
  • 1
  • 2
  • 3

3、 匹配开头路径

location ^~ /xxx/xx {
	#匹配所有以/xxx/xx开头的路径
}
  • 1
  • 2
  • 3
  • 1
  • 2
  • 3

4、正则匹配

location ~ /xxx {
	#匹配所有以/xxx开头的路径
}
  • 1
  • 2
  • 3
  • 1
  • 2
  • 3

5、匹配结尾路径

location ~* \.(gif/jpg/png)$ {
	#匹配以.gif、.jpg或者.png结尾的路径
}
  • 1
  • 2
  • 3
  • 1
  • 2
  • 3

6、 通用匹配

location / {
	#通用匹配,匹配所有请求
}
  • 1
  • 2
  • 3
  • 1
  • 2
  • 3

Nginx的负载均衡

Nginx有三种负载均和的策略

  1. 轮询:
    将客户端发起请求,平均分配给每一台服务器
  2. 权重:
    会将客户端的请求,根据服务器的权重值不同,分配不同的数量
  3. ip_hash:
    基于发起请求的客户端的ip地址不同,他始终会将请求发送到指定的服务器上
    就是说如果这个客户端的请求的ip地址不变,那么处理请求的服务器将一直是同一个

轮询

upstream daili_server{
    server localhost:8080; #服务器IP或域名
    server localhost:8081; #服务器IP或域名
}
server {
    listen       80;
    listen  [::]:80;
    server_name  localhost;

	location / {
        proxy_pass http://daili_server/;	#负载均衡
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13

权重

upstream daili_server{
    server localhost:8080 weight=10; #服务器IP或域名
    server localhost:8081 weight=2;  #服务器IP或域名
}
server {
    listen       80;
    listen  [::]:80;
    server_name  localhost;

	location / {


    

        proxy_pass http://daili_server/;	#负载均衡
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13

ip_hash

upstream daili_server{
	ip_hash;
    server localhost:8080; #服务器IP或域名
    server localhost:8081; #服务器IP或域名
}
server {
    listen       80;
    listen  [::]:80;
    server_name  localhost;

	location / {
        proxy_pass http://daili_server/;	#负载均衡
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14

Nginx动静分离

提高用户访问静态代码的速度,降低对后台应用访问
我们将静态资源放到nginx中,动态资源转发到tomcat服务器中
Nginx的并发能力公式:
worker_processes * worker_connections / 4|2 = Nginx最终的并发能力
动态资源需要/4,静态资源只需要/2

动态资源代理

location / {
  proxy_pass 路径;
}
  • 1
  • 2
  • 3
  • 1
  • 2
  • 3

静态资源代理

location / {
    root 静态资源路径;
    index 默认访问路径下的什么资源;
    autoindex on;#可以不写,写了则代表展示静态资源的全部内容,以列表的形式展开 
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 1
  • 2
  • 3
  • 4
  • 5

扩展内容(nginx集群)

Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/74930
 
338 次点击