Nginx学习记录(无集群)

一、Nginx简介

1、什么是nginx

Nginx 是高性能的 HTTP 和反向代理的服务器,处理高并发能力是十分强大的,能经受高负载的考验,有报告表

明能支持高达 50,000 个并发连接数。

2、反向代理

  • 正向代理:需要在客户端配置代理服务器进行指定网站访问。(客户端配置)
  • 反向代理:暴露的是代理服务器地址,隐藏了真实服务器 IP 地址。(服务端配置)

3、负载均衡

  • 负载均衡:增加服务器的数量,然后将请求分发到各个服务器上,将原先请求集中到单个服务器上的情况改为将请求分发到多个服务器上,将负载分发到不同的服务器

4、动静分离

  • 动静分离:后台应用的静态资源与动态资源分开部署。

二、nginx安装

1、安装

# ls
nginx-1.12.2.tar.gz 
pcre-8.37.tar.gz
  • 安装 pcre 依赖
1)tar –xvf pcre-8.37.tar.gz
2)./configure
3) make && make install
`检查版本`pcre-config -version
  • 安装 openssl 、zlib 、 gcc 依赖
yum -y install make zlib zlib-devel gcc-c++ libtool openssl openssl-devel
  • 安装 nginx
1)tar –xvf nginx-1.12.2.tar.gz 
2)./configure
3) make && make install
`检查版本`cd  /usr/local/nginx/sbin
         ./nginx -v
         ./nginx
  • 访问 ip+80

2、常用命令

  • 版本号
./nginx -v
  • 启动
./nginx
  • 关闭
./nginx -s stop
  • 重启
./nginx -s reload

3、配置文件

位置:/usr/local/nginx/conf/nginx.conf

  • 全局块:配置服务器整体运行的配置指令,比如 worker_processes 1; 处理并发数的配置
#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块:影响 Nginx服务器与用户的网络连接。比如 worker_connections 1024; 支持的最大连接数为 1024
events {
    
    
    worker_connections  1024;
}
  • http块:http 全局块+sever块
http {
    
    
    include       mime.types;
    default_type  application/octet-stream;
    server {
    
    
        listen       80;
        server_name  localhost;

        location / {
    
    
            root   html;
            index  index.html index.htm;
        }
		error_page   500 502 503 504  /50x.html;
        location = /50x.html {
    
    
            root   html;
        }
}

三、配置实例

1、反向代理

  • 端口转发
server {
    listen       80;
    server_name  localhost;

    location / {
    	proxy_pass http://192.168.22.203:8080/;
	}
}
  • 路径转发

路径优先级:(location =/index)>(location /xxx/xxx/xxx)>(location ^~/xxx)>(location ~/xxx)

=(location ~*.[jpg|png|gif])>( location /)

location =/index {
    
    
	#精准匹配,只匹配当前路径
	}
location /xxx/xxx/xxx {
    
    
    	#通用匹配,匹配/xxx开头的所有路径
	}
location ^~/xxx {
    
    
    	#正则匹配,匹配/xxx开头的所有路径
	}
location ~/xxx {
    
    
    	#匹配开头路径,匹配/xxx开头的所有路径
	}
location ~*\.[jpg|png|gif] {
    
    
    	#匹配结尾,匹配/xxx开头的所有路径
	}

2、负载均衡

  • 负载均衡
upstream my-server{
    
    
	server 192.168.22.203:8080;
	server 192.168.22.203:8081;
}
server {
    
    
    listen       80;
    server_name  localhost;    
    location / {
    
    
    	proxy_pass http://my-server/;
	}
}
  • 均衡策略

    • 均衡:平均转发到两天服务器
    upstream my-server{
          
          
    	server 192.168.22.203:8080;
    	server 192.168.22.203:8081;
    }
    
    • 权重:根据权重,处理速度快的权重大,分发的多
    upstream my-server{
          
          
    	server 192.168.22.203:8080 weight=10;
    	server 192.168.22.203:8081 weight=2;
    }
    
    • IP Hash:一个ip的请求始终交给一台服务器
    upstream my-server{
          
          
    	ip_hash;
    	server 192.168.22.203:8080;
    	server 192.168.22.203:8081;
    }
    

3、动静分离

  1. 动态资源代理:占用4个连接数
location / {
    
    
	proxy_pass 路径;
}
  1. 静态资源代理:占用两个连接数
location / {
    
    
	root 静态资源路径;
	index 默认访问路径下的资源;
	autoindex on;#展示静态资源的全部内容,以列表显示展开
}

猜你喜欢

转载自blog.csdn.net/qq_38473355/article/details/108708879
今日推荐