励志一下,加油 !!!
nginx
nginx能干什么?
- http服务器——能处理http协议
- 反向代理服务器
- 邮件服务器——IMAP/POP3/SMTP
正向代理和反向代理
正向代理——就是平时说的代理,比如vpn,比如公司内部的外网权限
描述:
- 代理内部网络对Internet的连接请求
- 客户机必须指定代理服务器
- 代理服务器是为用户服务的
反向代理
描述:
- 以代理服务器来接受Internet上的连接请求
- 将请求转发给内部网络上的服务器
- 将服务器上得到的结果返回给Internet上请求连接的客户
- 反向代理服务器是为web服务器服务的
二者的区别
正向代理:
- 典型用途是为在防火墙内的局域网客户端提供访问Internet的途径。
- 正向代理还可以使用缓冲特性减少网络使用率。
反向代理
- 典型用途是将防火墙后面的服务器提供给Internet用户访问。
- 反向代理还可以为后端的多台服务器提供负载平衡,或为后端较慢的服务器提供缓冲服务。
nginx的安装
Nginx相关依赖:
- OpenSSL: ——数据加密
- PCRE——解析正则表达式
- ZLib——压缩解压缩
获取链接:https://pan.baidu.com/s/11dGMjLSojIIVGQnJRt8_nQ
安装之后
启动nginx
开启了两个进程,一个是管理者进程,一个是干货的进程,两个进程都要开启。
nginx操作命令
1、进入到安装目录:
/usr/local/nginx/sbin
2、启动:
sudo ./nginx
3、停止:
sudo ./nginx -s stop - -暴力,马上终止web服务器
sudo ./nginx -s quit -- 温柔,等待当前操作处理完成之后再停止
4、重新加载:
sudo ./nginx -s reload
nginx的配置文件
Nginx配置文件的位置:
/usr/local/nginx/conf
文件名
nginx.conf
配置文件的组织结构
http模块——: 处理web请求
- server: 一个server就代表一个web服务器。一个http里面可以有多个server
- location:——代表web服务器要出来的指令是什么,一个location就代表一个指令
mail模块——: 处理邮件相关的协议
nginx设置静态页面
将静态页面放在与html目录平级的目录中,路径如下
比如新加入的目录名为 demo,设置的静态网页名为second.html。则进行如下设置
#second..html要访问的静态网页的名字
location /second..html{
#资源目录名
root demo;
index second.html;
}
修改完conf文件之后,重启nginx服务器,即可查看到设置的静态网页。
同理,下面设置的静态页面资源位置在demo目录下的hello目录下的index.html(注意,location后面写的目录的后面一定要加“/”,不是目录可以不加)
location /hello/index.html{
root demo;
index index.html;
}
域名和IP的相关知识点
一个IP可以被多个域名绑定。一个域名只能绑定一个IP。
输入www.baidu.com之后会发生什么
先通过DNS服务器查找域名www.baidu.com的ip,得到一个百度服务器IP。浏览器在通过IP去连接百度服务器。
hosts文件
为了加快域名解析速度,该文件在本地,如果host文件中存在域名的ip地址,则直接使用本地host文件中的ip,而不用去通过DNS代理服务器去获取IP地址。
nginx设置反向代理
反向代理的前提是有多台web服务器。
第一步:在nginx.conf文件的http下,设置反向代理服务
# 配置要代理的服务器信息
# luffy - 192.168.82.251
server {
# 监听的端口,一个服务器需要监听一个端口
listen 80;
# server_name对应一个域名。rudy.com是一个域名
server_name rudy.com;
location / {
#设置代理,反向代理的的名字随便起,但是前面一定要加http://
proxy_pass http://rudy.com
}
第二步,设反向代理(在server的上方写)
# 设置反向代理
upstream rudy.com{
server 192.168.82.253:80;
}
反向代理部分的整体
# 设置反向代理
upstream rudy.com{
server 192.168.82.253:80;
}
upstream luffy.com{
server 192.168.82.251:80;
}
# rudy - 192.168.82.253
server {
# 监听的端口
listen 80;
# 通过什么域名去访问当前的web服务器
server_name rudy.com;
location / {
# 设置代理
proxy_pass http://rudy.com;
}
}
# luffy - 192.168.82.251
server {
# 监听的端口
listen 80;
# 通过什么域名去访问当前的web服务器
server_name luffy.com;
location / {
# 设置代理
proxy_pass http://luffy.com;
}
}
反向代理的流程:
- 通过浏览器访问server模块中的server_name对应的域名(比如的绗棉的rudy.com)
- 服务器去找loacation /
- 需要在里边设置代理: proxy_pass + url
- 通过这个url的名字去找upstream模块
- 找到之后, 去访问该模块中server对应的地址
负载均衡——在反向代理的基础上做的
在一个upstream中设置多个ip即是负载均衡
1、默认形式,两个ip等机会被访问
upstream rudy.com{
server 192.168.82.253:80;
server 192.168.82.251:80;
}
2、权重形式(weight没有最大值,最小值是1.下面代码中,253和251被访问的比率是5:1)
upstream rudy.com{
server 192.168.82.253:80 weight=5;
server 192.168.82.251:80 weight=1;
}
负载均衡的完整代码
upstream rudy.com{
server 192.168.82.253:80 weight=5;
server 192.168.82.251:80 weight=1;
}
server {
# 监听的端口
listen 80;
# 通过什么域名去访问当前的web服务器
server_name rudy.com;
location / {
# 设置代理
proxy_pass http://rudy.com;
}
}
负载均衡的流程
- 入口: server模块里边的server_name对应的域名
- 进入到location /
- 发现是一个代理: 里边的proxy_pass有一个url
- 通过这个url的名字去找upstream
- 所有的web服务器的地址都在upstream里边,默认情况下会理请求的机会均等,也可以设置权重:
自己的nginx.conf文件
#nginx进程数,如果没有阻塞式调用,建议进程个数和CPU内核数相同,如果有阻塞式调用,建议比CPU内核数稍大一点
worker_processes auto;
# 错误日志的位置和级别,[ debug | info | notice | warn | error | crit ]
error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#进程文件
pid logs/nginx.pid;
#修改worker进程的最大打开文件数限制
worker_rlimit_nofile 65535;
#使nginx不能生成后台进程,如此,supervisor就可以监听他了
daemon off;
events {
#参考事件模型,use [ kqueue | rtsig | epoll | /dev/poll | select | poll ];
#epoll模型是Linux 2.6以上版本内核中的高性能网络I/O模型,如果跑在FreeBSD上面,就用kqueue模型。
#use epoll;
#单个进程最大连接数(最大连接数=连接数*进程数)
worker_connections 65535;
#set to 'on' if nginx work_processes more than 1
accept_mutex on;
#使用epoll的I/O 模型。linux建议epoll,FreeBSD建议采用kqueue
use epoll;
}
# 设定http服务器
http {
include mime.types;
#fallback in case we can't determine a type'
default_type application/octet-stream;
# 日志格式设定, 其中main为格式的名称,可以设置多个不同的格式
# 可输出的各字段,及各字段含义可自行搜索
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
# 定义访问日志, main为该文件输出的日志格式
# 如果server中没有指定单独的输出文件,则统一按照此文件、配置输出
access_log logs/access.log main;
sendfile on;
#tcp_nopush on;
#keepalive_timeout 0;
#长连接超时时间,单位是秒
keepalive_timeout 65;
#hash bucket size总是等于保存服务器名字的hash表的大小
server_names_hash_bucket_size 128;
#client_header_buffer_size 16k;
#开启gzip压缩输出
#gzip on;
#large_client_header_buffers 8 128k;
#server{
#if no Host match, close the connection to prevent host spoofing
#listen 8891 default_server;
#return 444;
#}
#负载均衡
upstream gunicorn {
server 127.0.0.1:8889;
}
server {
# 监听端口, 监听80端口时, 必须用root账户启动nginx
# use 'listen 80 deferred;' for Linux''
listen 8888;
# 域名可以有多个,用空格隔开
server_name localhost;
client_max_body_size 4G;
#charset koi8-r;
#access_log logs/host.access.log main;
keepalive_timeout 5;
#path for static files(静态文件就加载这里的了)
#root /home/disk2/visionary_platform/static/images;
#客户端缓冲头大小
client_header_buffer_size 16k;
client_body_buffer_size 32k;
#客户请求头缓冲过大时自动调用
large_client_header_buffers 8 128k;
# server作为静态web服务器,服务重定向到本地的静态资源目录
# "/" 可以替换为其他表达式
# 以下配置,则访问http://localhost的时候,请求重定向到nginx安装目录下html中的index.html""
location / {
#反向代理
#监听8888端口,一旦有访问就反向代理带8889端口(该端口是gunicorn开启的visionary进程所用的端口)
proxy_pass http://gunicorn;
# root指定重定向后uri的资源查找路径,这里html为相对路径,相对于nginx的安装目录
# 默认在nginx的安装目录中会有一个html目录,里面有index.html, 50x.html文件
#root html;
# index指定首页index文件的名称,可以配置多个,参数以空格分开,按配置顺序查找
#index index.html index.htm;
#index visionary.html;
}
#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;
}
# 静态资源加载(此处用于加载visionary图片。还可以加载静态html页面,css,js等资源)
# 与server配合。当监听到该server来提供服务,则由Location来提供资源
# root directive is placed in the server context
# matching all URIs ending with .gif, .jpg, or .png
#location ~ \.(gif|jpg|png)$ {
# root /home/disk2/visionary_platform/static/images;
#}
# 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块,但是两个server块的配置中listen和server配置不能一样,否则报错
#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 ssl;
# server_name localhost;
# ssl_certificate cert.pem;
# ssl_certificate_key cert.key;
# ssl_session_cache shared:SSL:1m;
# ssl_session_timeout 5m;
# ssl_ciphers HIGH:!aNULL:!MD5;
# ssl_prefer_server_ciphers on;
# location / {
# root html;
# index index.html index.htm;
# }
#}
}