Nginx
Nginx 配置详解
序⾔
Nginx是lgor Sysoev为俄罗斯访问量第⼆的rambler.ru站点设计开发的。从2004年发布⾄今,凭借开源的⼒量,已经接近成熟与完善。Nginx功能丰富,可作为HTTP服务器,也可作为反向代理服务器,邮件服务器。⽀持FastCGI、SSL、Virtual Host、URL Rewrite、Gzip等功能。并且⽀持很多第三⽅的模块扩展。
Nginx的稳定性、功能集、⽰例配置⽂件和低系统资源的消耗让他后来居上,在全球活跃的⽹站中有12.18%的使⽤⽐率,⼤约为2220万个⽹站。
⽜逼吹的差不多啦,如果你还不过瘾,你可以百度百科或者⼀些书上到这样的夸耀,⽐⽐皆是。
Nginx常⽤功能
1、Http代理,反向代理:作为web服务器最常⽤的功能之⼀,尤其是反向代理。
这⾥我给来2张图,对正向代理与反响代理做个诠释,具体细节,⼤家可以翻阅下资料。
Nginx在做反向代理时,提供性能稳定,并且能够提供配置灵活的转发功能。Nginx可以根据不同的正则匹配,采取不同的转发策略,⽐如图⽚⽂件结尾的⾛⽂件服务器,动态页⾯⾛web服务器,只要你正则写的没问题,⼜有相对应的服务器解决⽅案,你就可以随⼼所欲的玩。并且Nginx对返回结果进⾏错误页跳转,异常判断等。如果被分发的服务器存在异常,他可以将请求重新转发给另外⼀台服务器,然后⾃动去除异常服务器。
2、负载均衡
Nginx提供的负载均衡策略有2种:内置策略和扩展策略。内置策略为轮询,加权轮询,Ip hash。扩展策略,就天马⾏空,只有你想不到的没有他做不到的啦,你可以参照所有的负载均衡算法,给他⼀⼀出来做下实现。
上3个图,理解这三种负载均衡算法的实现
Ip hash算法,对客户端请求的ip进⾏hash操作,然后根据hash结果将同⼀个客户端ip的请求分发给同⼀台服务器进⾏处理,可以解决session不共享的问题。
3、web缓存
Nginx可以对不同的⽂件做不同的缓存处理,配置灵活,并且⽀持FastCGI_Cache,主要⽤于对FastC
GI的动态程序进⾏缓存。配合着第三⽅的ngx_cache_purge,对制定的URL缓存内容可以的进⾏增删管理。
4、Nginx相关地址
Nginx配置⽂件结构
如果你下载好啦,你的安装⽂件,不妨打开conf⽂件夹的f⽂件,Nginx服务器的基础配置,默认的配置也存放在此。
在 f 的注释符号为: #
默认的 nginx 配置⽂件 f 内容如下:
user www-data;
worker_processes auto;
pid /run/nginx.pid;
events {
worker_connections 768;
# multi_accept on;
}
http {
##
# Basic Settings
##
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
types_hash_max_size 2048;
# server_tokens off;
# server_names_hash_bucket_size 64;
# server_name_in_redirect off;
include /etc/pes;
default_type application/octet-stream;
##
# SSL Settings
##
ssl_protocols TLSv1 TLSv1.1 TLSv1.2; # Dropping SSLv3, ref: POODLE
ssl_prefer_server_ciphers on;
##
# Logging Settings
##
access_log /var/log/nginx/access.log;
error_log /var/log/nginx/error.log;
##
# Gzip Settings
##
gzip on;
gzip_disable "msie6";
# gzip_vary on;
# gzip_proxied any;
# gzip_comp_level 6;
# gzip_buffers 16 8k;
# gzip_http_version 1.1;
# gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript; ##
# Virtual Host Configs
##
include /etc/nginx/conf.d/*.conf;
include /etc/nginx/sites-enabled/*;
}
#mail {
# # See sample authentication script at:
# # /ImapAuthenticateWithApachePhpScript
#
# # auth_http localhost/auth.php;
# # pop3_capabilities "TOP" "USER";
# # imap_capabilities "IMAP4rev1" "UIDPLUS";
#
# server {
# listen localhost:110;
# protocol pop3;
# proxy on;
# }
#
# server {
# listen localhost:143;
# protocol imap;
# proxy on;
# }
#}
项⽬中常⽤的nginx配置
upstream app-backend {
server 127.0.0.1:8000;
}
upstream websockify-backend {
server 127.0.0.1:6080;
}
upstream container-backend {
server 127.0.0.1:7080;
}
upstream chat-backend {
server 127.0.0.1:8080;
}
server {
listen 80;
listen [::]:80;
server_name localhost;
index index.html;
error_page 404 = /;
client_max_body_size 10G;
charset UTF-8;
location ^~ /static/ {
alias /var/www/static/;
}
location ^~ /media/ {
alias /var/www/media/;
}
location ~ \. {
root /var/www/public;
}
location /chat {
proxy_pass chat-backend;
proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header X-Forwarded-Proto $scheme;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_read_timeout 1d;
proxy_buffering off;
}
location /ws {
proxy_pass container-backend;
proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header X-Forwarded-Proto $scheme;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_read_timeout 1d;
proxy_buffering off;
}
location /websockify {
proxy_pass websockify-backend;
proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_read_timeout 1d;
proxy_buffering off;
}
location / {
try_files $uri @proxy_to_app;
}
location @proxy_to_app {
proxy_pass app-backend;
proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header X-Forwarded-Proto $scheme;
}
}
server {
listen 443;
listen [::]:443;
ssl on;
ssl_certificate /var/www/x/;
ssl_certificate_key /var/www/x/nginx/x.key;
server_name localhost;
index index.html;
error_page 404 = /;
client_max_body_size 10G;
charset UTF-8;
location ^~ /static/ {
alias /var/www/static/;
}
location ^~ /media/ {
alias /var/www/media/;
}
location ~ \. {
root /var/www/public;
}
location /chat {
proxy_pass chat-backend;
proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header X-Forwarded-Proto $scheme;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_read_timeout 1d;
proxy_buffering off;
}
location /ws {
proxy_pass container-backend;
proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header X-Forwarded-Proto $scheme;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_buffering off;
}
location /websockify {
proxy_pass websockify-backend;
proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_read_timeout 1d;
proxy_buffering off;
}
location / {
try_files $uri @proxy_to_app;
}
location @proxy_to_app {
proxy_pass app-backend;
proxy_set_header Host $http_host;
nginx 配置文件proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
}
}
nginx负载均衡配置⽂件
.
只需要在原有的配置⽂件中添加⼀下配置即可
.
upstream backend {
server 10.1.0.101;
server 10.1.0.102;
server 10.1.0.103;
}
上⾯是nginx的基本配置,需要注意的有以下⼏点:
1、⼏个常见配置项:
1.$remote_addr 与 $http_x_forwarded_for ⽤以记录客户端的ip地址;
2.$remote_user :⽤来记录客户端⽤户名称;
3.$time_local :⽤来记录访问时间与时区;
4.$request :⽤来记录请求的url与http协议;
5.$status :⽤来记录请求状态;成功是200;
6.$body_bytes_s ent :记录发送给客户端⽂件主体内容⼤⼩;
7.$http_referer :⽤来记录从那个页⾯链接访问过来的;
8.$http_user_agent :记录客户端浏览器的相关信息;
2、惊现象:⼀个⽹路连接到来,多个睡眠的进程被同时叫醒,但只有⼀个进程能获得链接,这样会影响系统性能。
3、每个指令必须有分号结束。
4、Nginx 的 upstream⽬前⽀持的分配算法:
1.轮询 ——1:1 轮流处理请求(默认)
每个请求按时间顺序逐⼀分配到不同的应⽤服务器,如果应⽤服务器down掉,⾃动剔除,剩下的继续轮询。
2.权重 ——you can you up
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论