nginx-学习(一)
目录
一、nginx
1.nginx的简介
根据现在的分布式项目的应用场景越拉越多,且需要对服务进行冗余部署,nginx也变得越来越火,nginx具有三大核心功能:
- 反向代理
- 负载均衡
- 静态资源服务器
2.nginx的安装并快速启动
启动之前必须安装docker-compose
在 cd /usr/local/docker下新建文件夹 mkdir nginx
然后在 /usr/local/docker/nginx目录下:vi docker-compose.yml
使用docker-compose.yml快速启动
version: '3.1'
services:
nginx:
restart: always
image: daocloud.io/library/nginx:latest
container_name: nginx
ports:
- 80:80
使用命令:docker-compose up -d ,使配置文件运行
3.nginx的核心配置文件 nginx.conf
nginx.conf
user nginx;
worker_processes 1; # 跟并发量有关,运维的同学负责
error_log /var/log/nginx/error.log warn;
pid /var/run/nginx.pid;
events {
worker_connections 1024; # 跟并发量相关
}
http {
include /etc/nginx/mime.types; # 引入mime类型的配置文件
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;
include /etc/nginx/conf.d/*.conf; # 引入conf.d内的所有配置文件
}
conf.d/default.conf
server { #server块就是一个具体的项目
listen 80; #nginx监听的端口
listen [::]:80;
server_name localhost; #该服务具体的ip地址
#charset koi8-r;
#access_log /var/log/nginx/host.access.log main;
location / { # 要拦截的路径: / 根路径
root /usr/share/nginx/html; # 在根路径下拦截的内容会去/usr/share/nginx/html寻找静态资源
index index.html index.htm; #默认页面 /usr/share/nginx/html目录内找默认页面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 /usr/share/nginx/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;
#}
}
这两个文件合并后的效果:
user nginx;
worker_processes 1;
error_log /var/log/nginx/error.log warn;
pid /var/run/nginx.pid;
events {
worker_connections 1024;
}
http {
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;
keepalive_timeout 65;
//下面是conf.d/default.conf内容
server {
listen 80;
listen [::]:80;
server_name localhost;
location / {
root /usr/share/nginx/html;
index index.html index.htm;
}
}
}
4.带配置文件的快速启动
1)创建nginx的配置文件
user nginx;
worker_processes 1;
error_log /var/log/nginx/error.log warn;
pid /var/run/nginx.pid;
events {
worker_connections 1024;
}
http {
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;
keepalive_timeout 65;
server {
listen 80;
listen [::]:80;
server_name localhost;
location / {
root /usr/share/nginx/html;
index 1.html;
}
}
}
2)创建静态资源路径
3)编写docker-compose.yml文件(重点)
nginx文件夹下:wwwroot, conf/nginx.conf, docker-compose.yml
version: '3.1'
services:
nginx:
restart: always
image: daocloud.io/library/nginx:latest
container_name: nginx
ports:
- 8080:8080
volumes:
- /usr/local/docker/nginx/conf/nginx.conf:/etc/nginx/nginx.conf # 挂载配置文件
#前面部分是自己创建的配置文件(如上):映射官方的配置文件
- /usr/local/docker/nginx/wwwroot/:/usr/share/nginx/html/ #挂载静态资源路径
5.nginx实现反向代理
1)什么是正向代理和反向代理
- 正向代理: 客户端主动设置的代理服务器,比如要FQ,主动设置代理服务器来获取墙外资源
- 反向代理:服务器端设置的代理服务器,向客户端屏蔽服务器端的服务器的部署结构。
2)如何实现反向代理
很简单,修改nginx的配置文件-----ngnix.conf
server{
listen 80;
server_name localhost;
# 基于反向代理访问到Tomcat服务器
location / {
proxy_pass http://192.168.199.109:8080/;# 要代理到的目标服务器地址(通过本地映射到此路径)
}
}
6.nginx的负载均衡
官方提供了三种负载均衡策略:
- 轮询: 默认的负载均衡策略: 多个请求轮流访问后端反向代理的服务器。
- 权重:根据权重比例,后端服务器收到的请求比例是设置的权重比例。
- ip_hash: 用户请求哪一个后端服务器,是由用户的ip地址决定,nginx通过ip地址的hash算法,算出访问后端的哪一个服务器。
三种负载均衡的实现:修改ngnix.conf中的配置文件(添加upstream的内容)
- 轮询
user nginx;
worker_processes 1;
error_log /var/log/nginx/error.log warn;
pid /var/run/nginx.pid;
events {
worker_connections 1024;
}
http {
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;
keepalive_timeout 65;
upstream ssm { # ssm 是当前后端服务器集群的名字
server 192.168.241.132:9091;
server 192.168.241.132:9092;
}
server {
listen 80;
listen [::]:80;
server_name localhost;
location / {
proxy_pass http://ssm/; # 设置服务器集群的名字
}
}
}
- 权重
user nginx;
worker_processes 1;
error_log /var/log/nginx/error.log warn;
pid /var/run/nginx.pid;
events {
worker_connections 1024;
}
http {
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;
keepalive_timeout 65;
upstream ssm {
server 172.16.253.4:9091 weight=10; # 带着权重比例
server 172.16.253.4:8081 weight=90;
}
server {
listen 80;
listen [::]:80;
server_name localhost;
location / {
proxy_pass http://ssm/;
}
}
}
- ip_hash
user nginx;
worker_processes 1;
error_log /var/log/nginx/error.log warn;
pid /var/run/nginx.pid;
events {
worker_connections 1024;
}
http {
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;
keepalive_timeout 65;
upstream ssm {
ip_hash; #使用ip_hash的负载均衡策略
server 172.16.253.4:9091;
server 172.16.253.4:8081;
}
server {
listen 80;
listen [::]:80;
server_name localhost;
location / {
proxy_pass http://ssm/;
}
}
}
7.Nginx动静分离【重点
】
Nginx的并发能力公式:
worker_processes * worker_connections / 4 | 2 = Nginx最终的并发能力
动态资源需要/4,静态资源需要/2.
Nginx通过动静分离,来提升Nginx的并发能力,更快的给用户响应。
7.1 动态资源代理
使用proxy_pass动态代理
# 配置如下
location / {
proxy_pass 路径;
}
7.2 静态资源代理
使用root静态代理
# 配置如下
location / {
root 静态资源路径;
index 默认访问路径下的什么资源;
autoindex on; # 代表展示静态资源全的全部内容,以列表的形式展开。
}
# 先修改docker,添加一个数据卷,映射到Nginx服务器的一个目录
# 添加了index.html和1.jpg静态资源
# 修改配置文件