一、安装nginx
yum install -y nginx
二、修改nginx配置
1.主配置/etc/nginx/nginx.conf
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
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 logs/access.log main;
include /etc/nginx/conf.d/*.conf;
#include proxy.conf;
#include upstrem.conf;
#include blog.biglittleant.cn.conf;
server {
listen 80;
server_name localhost;
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
}
2.proxy配置/etc/nginx/conf.d/proxy.conf
proxy_temp_path /data/cdn_cache/proxy_temp_dir;
proxy_cache_path /data/cdn_cache/proxy_cache_dir levels=1:2 keys_zone=cache_one:50m inactive=1d max_size=1g;
proxy_connect_timeout 5;
proxy_read_timeout 60;
proxy_send_timeout 5;
proxy_buffer_size 16k;
proxy_buffers 4 64k;
proxy_busy_buffers_size 128k;
proxy_temp_file_write_size 128k;
proxy_next_upstream error timeout invalid_header http_500 http_502 http_503 http_404;
参数解释:
proxy_cache_path: 缓存文件路径
levels: 设置缓存文件目录层次;levels=1:2 表示两级目录
keys_zone: 设置缓存名字和共享内存大小
inactive: 在指定时间内没人访问则被删除
max_size: 最大缓存空间,如果缓存空间满,默认覆盖掉缓存时间最长的资源。每一个proxy_cache_path对应一个ngx_http_file_cache_t结构体。
proxy_cache tmp-test: 使用名为tmp-test的缓存配置
proxy_cache_key $uri :定义缓存唯一key,通过唯一key来进行hash存取
proxy_cache_methods :设置缓存哪些HTTP方法
proxy_cache_min_uses :指定请求至少被发送了多少次以上时才缓存,可以防止低频请求被缓存
proxy_cache_bypass :如果指定的任何一个变量值不为空,或者不等于0,nginx就不会查找缓存,直接进行代理转发
proxy_cache_lock/proxy_cache_lock_timeout: 当多个客户端同时请求同一份内容时,如果开启proxy_cache_lock(默认off)则只有一个请求被发送至后端;其他请求将等待该内容返回;当第一个请求返回时,其他请求将从缓存中获取内容返回;当第一个请求超过了proxy_cache_lock_timeout超时时间(默认5s),则其他请求将同时请求到后端来获取响应,且响应不会被缓存;启用proxy_cache_lock可以应对雪崩效应。
3.upstream配置/etc/nginx/conf.d/upstream.conf
upstream blog.test.cn
{
server 47.75.246.12:80 weight=10 max_fails=3;
}
4.blog.test.cn配置/etc/nginx/conf.d/blog.test.cn.conf
server
{
listen 80;
server_name blog.test.cn;
access_log logs/blog.biglittleant.cn-access.log main;
location ~ .*\.(gif|jpg|png|html|htm|css|js|ico|swf|pdf|txt)$
{
#Proxy
proxy_redirect off;
proxy_next_upstream http_502 http_504 http_404 error timeout invalid_header;
proxy_set_header Host $host;
proxy_set_header X-real-ip $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass http://blog.test.cn;
#Use Proxy Cache
proxy_cache cache_one;
proxy_cache_key "$host$request_uri";
add_header Cache "$upstream_cache_status";
proxy_cache_valid 200 304 301 302 8h;
proxy_cache_valid 404 1m;
proxy_cache_valid any 2d;
}
location /
{
proxy_redirect off;
proxy_next_upstream http_502 http_504 http_404 error timeout invalid_header;
proxy_set_header Host $host;
proxy_set_header X-real-ip $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass http://blog.test.cn;
client_max_body_size 40m;
client_body_buffer_size 128k;
proxy_connect_timeout 60;
proxy_send_timeout 60;
proxy_read_timeout 60;
proxy_buffer_size 64k;
proxy_buffers 4 32k;
proxy_busy_buffers_size 64k;
}
}
新建存储目录:
mkdir -p /data/cdn_cache
注意:启动nginx会多出两个cache的进程。
第一次请求资源会先从源服务下载在nginx上,再返回给客户端。第二次请求相同资源时直接从nginx返回给客户端。
一、安装nginx
yum install -y nginx
二、修改配置文件/etc/nginx/nginx.conf
user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;
include /usr/share/nginx/modules/*.conf;
events {
worker_connections 1024;
}
http {
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;
tcp_nodelay on;
keepalive_timeout 65;
types_hash_max_size 2048;
include /etc/nginx/mime.types;
default_type application/octet-stream;
# Load modular configuration files from the /etc/nginx/conf.d directory.
# See http://nginx.org/en/docs/ngx_core_module.html#include
# for more information.
include /etc/nginx/conf.d/*.conf;
server {
listen 80 default_server;
listen [::]:80 default_server;
server_name _;
# Load configuration files for the default server block.
include /etc/nginx/default.d/*.conf;
location / {
root /usr/share/nginx/html/download;
autoindex on; #开启索引功能
autoindex_exact_size off; #关闭计算文件确切大小(单位bytes),只显示大概大小(单位kb、mb、gb)
autoindex_localtime on; #显示本机时间而非 GMT 时间
}
error_page 404 /404.html;
location = /40x.html {
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
}
}
}
三、创建下载路径/usr/share/nginx/html/download
mkdir -p /usr/share/nginx/html/download
四、上传文件并授权755
五、浏览器打开ngixn地址就可以愉快的下载了!!!
cd /etc/nginx/conf.d/
#创建密码文件和用户密码
htpasswd -c .htpasswd zskybio
配置文件增加
location /sdjd {
#编码设置为UTF-8,不然中文文件名乱码
charset utf-8;
alias /opt/shendujindiao/;
index index.html index.htm;
#列表
autoindex on;
#隐藏真实大小,以M或G显示
autoindex_exact_size off;
#显示时间
autoindex_localtime on;
#设置密码文件
auth_basic "input you user name and password";
auth_basic_user_file /etc/nginx/conf.d/.htpasswd;
}
重新加载服务
nginx -t
nginx -s reload
一、安装nginx
yum install -y nginx
二、修改配置文件/etc/nginx/nginx.conf
#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 {
worker_connections 1024;
}
http {
#正向代理
server {
#正向代理的端口
listen 9080;
#dns,支持配置多个
resolver 119.29.29.29;
#开启插件支持https tunnel
#proxy_connect;
#proxy_connect_allow 443;
#proxy_connect_connect_timeout 10s;
#proxy_connect_read_timeout 40s;
#proxy_connect_send_timeout 40s;
#location / {
# proxy_pass http://$host;
# proxy_set_header Host $host;
# proxy_buffers 256 4k;
# proxy_max_temp_file_size 0;
#}
}
#反向代理上游服务器-反向代理tpp-zuul-pre,支持配置多个
upstream srv_tpp-zuul-pre {
ip_hash;
server 172.168.168.108:80;
server 172.168.168.114:80;
}
#反向代理
server {
listen 8080;
listen 443 ssl;
#ssl on;
ssl_certificate 1613208__hcepay.com.pem;
ssl_certificate_key 1613208__hcepay.com.key;
ssl_session_timeout 5m;
ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_prefer_server_ciphers on;
server_name 127.0.0.1;
location / {
proxy_pass http://srv_tpp-zuul-pre;
}
}
upstream srv_fama
{
ip_hash;
server 172.168.168.112:8080;
server 172.168.168.119:8080;
}
server {
listen 8081;
server_name 127.0.0.1;
location / {
proxy_redirect off;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For proxy_add_x_forwarded_for;
proxy_pass http://srv_fama;
}
}
upstream srv_acc-pre
{
ip_hash;
server 172.168.168.107:8764;
server 172.168.168.113:8764;
}
server {
listen 8764;
server_name 127.0.0.1;
location / {
proxy_redirect off;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For proxy_add_x_forwarded_for;
proxy_pass http://srv_acc-pre;
}
}
upstream srv_redis
{
ip_hash;
server 172.168.168.119:7000;
server 172.168.168.119:7001;
server 172.168.168.119:7002;
server 172.168.168.119:7003;
server 172.168.168.119:7004;
server 172.168.168.119:7005;
}
server {
listen 6379;
server_name 127.0.0.1;
location / {
proxy_redirect off;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For proxy_add_x_forwarded_for;
proxy_pass http://srv_redis;
}
}
}
#TCP方向代理
tcp {
upstream srv_tpp-webgate {
ip_hash;
server 172.168.168.108:5001;
server 172.168.168.114:5001;
check interval=3000 rise=2 fall=5 timeout=1000;
}
server {
listen 5001;
proxy_pass srv_tpp-webgate;
tcp_nodelay on;
}
}
proxy模块指令描述
proxy模块的可用配置指令非常多,它们分别用于定义proxy模块工作时的诸多属性,如连接超时时长、代理时使用http协议版本等。下面对常用的指令做一个简单说明。
如下面的一个示例:
proxy_redirect off;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
client_max_body_size 10m;
client_body_buffer_size 128k;
proxy_connect_timeout 30;
proxy_send_timeout 15;
proxy_read_timeout 15;
1、轮询(默认)
每个请求按时间顺序逐一分配到不同的后端服务器,如果后端服务器down掉,能自动剔除。
upstream backserver {
server 192.168.0.14;
server 192.168.0.15;
}
2、指定权重
指定轮询几率,weight和访问比率成正比,用于后端服务器性能不均的情况。
upstream backserver {
server 192.168.0.14 weight=10;
server 192.168.0.15 weight=10;
}
3、IP绑定 ip_hash
每个请求按访问ip的hash结果分配,这样每个访客固定访问一个后端服务器,可以解决session的问题。
upstream backserver {
ip_hash;
server 192.168.0.14:88;
server 192.168.0.15:80;
}
4、fair(第三方)
按后端服务器的响应时间来分配请求,响应时间短的优先分配。
upstream backserver {
server server1;
server server2;
fair;
}
5、url_hash(第三方)
按访问url的hash结果来分配请求,使每个url定向到同一个后端服务器,后端服务器为缓存时比较有效。
upstream backserver {
server squid1:3128;
server squid2:3128;
hash $request_uri;
hash_method crc32;
}
在需要使用负载均衡的server中增加
proxy_pass http://backserver/;
upstream backserver{
ip_hash;
server 127.0.0.1:9090 down; (down 表示单前的server暂时不参与负载)
server 127.0.0.1:8080 weight=2; (weight 默认为1.weight越大,负载的权重就越大)
server 127.0.0.1:6060;
server 127.0.0.1:7070 backup; (其它所有的非backup机器down或者忙的时候,请求backup机器)
}
max_fails :允许请求失败的次数默认为1.当超过最大次数时,返回proxy_next_upstream 模块定义的错误
fail_timeout:max_fails次失败后,暂停的时间
在需要使用负载均衡的server中增加
proxy_pass http://backserver/;
upstream backserver{
ip_hash;
server 127.0.0.1:9090 down; (down 表示单前的server暂时不参与负载)
server 127.0.0.1:8080 weight=2; (weight 默认为1.weight越大,负载的权重就越大)
server 127.0.0.1:6060;
server 127.0.0.1:7070 backup; (其它所有的非backup机器down或者忙的时候,请求backup机器)
}
max_fails :允许请求失败的次数默认为1.当超过最大次数时,返回proxy_next_upstream 模块定义的错误
fail_timeout:max_fails次失败后,暂停的时间
[外链图片转存中…(img-mMHAi2Z3-1594631525459)]