Nginx配置文件nginx |
您所在的位置:网站首页 › nginx配置多个proxy_pass › Nginx配置文件nginx |
1. 基础配置文件
# 定义nginx运行的用户和用户组
#user nobody;
#nginx进程数,一般可设置为CPU核心数 -1
worker_processes 1;
#全局错误日志位置,错误类型有:[ debug | info | notice | warn | error | crit ]
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
# 进程文件 进程id
#pid logs/nginx.pid;
events {
#单个进程最大连接数(最大连接数=连接数*进程数)
worker_connections 1024;
}
# 设置http服务器
http {
# include引入外部文件,mime.types文件扩展名与文件类型映射表
include mime.types;
# 默认文件类型
default_type application/octet-stream;
# 日志文件输出格式 , main 为定义的格式名称,如此 access_log 就可以直接使用这个变量了
#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;
# 开启高效文件传输模式,sendfile指令指定nginx是否调用sendfile函数来输出文件,对于普通应用设为 on,如果用来进行下载等应用磁盘IO重负载应用,可设置为off,以平衡磁盘与网络I/O处理速度,降低系统的负载.注意:如果图片显示不正常把这个改成off。启用后才能使用 tcp_nopush ,是指当数据表累积一定大小后才发送,提高了效率。
sendfile on;
#tcp_nopush on;
# 设置客户端与服务端请求的超时时间,保证客户端多次请求的时候不会重复建立新的连接,节约资源损耗。
#keepalive_timeout 0;
keepalive_timeout 65;
# gizp模块,开启gzip压缩
#gzip on;
# 虚拟主机的配置
server {
# 监听端口
listen 80;
# 域名,可以有多个,用空格隔开
server_name localhost;
# 编码
#charset koi8-r;
# 虚拟主机日志文件
#access_log logs/host.access.log main;
location / {
root html;
index 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 html;
}
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
# 代理所有PHP脚本到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
# 将 PHP 脚本传递给在 127.0.0.1:9000 上监听的 FastCGI 服务器
#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
# 则拒绝访问 .htaccess 文件
#location ~ /\.ht {
# deny all;
#}
}
# another virtual host using mix of IP-, name-, and port-based configuration
#
#server {
# listen 8000;
# listen somename:8080;
# server_name somename alias another.alias;
# location / {
# root html;
# index index.html index.htm;
# }
#}
# HTTPS server
# 监听端口 HTTPS
#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;
# }
#}
}
日志文件参数含义: $remoteaddr与$http_x_forwarded_for 用以记录客户端的ip地址; $remote_user :用来记录客户端用户名称; $time_local : 用来记录访问时间与时区; $request : 用来记录请求的url与http协议; $status : 用来记录请求状态;成功是200, $body_bytes_s ent :记录发送给客户端文件主体内容大小; $http_referer :用来记录从那个页面链接访问过来的; $http_user_agent :记录客户端浏览器的相关信息; 2. 跨域请求配置 server { # 监听端口 listen 80; # 域名,可以有多个,用空格隔开 server_name localhost; #允许跨域请求的域,*代表所有 add_header 'Access-Control-Allow-Origin' *; #允许带上cookie请求 add_header 'Access-Control-Allow-Credentials' 'true'; #允许请求的方法,比如 GET/POST/PUT/DELETE add_header 'Access-Control-Allow-Methods' *; #允许请求的header add_header 'Access-Control-Allow-Headers' *; # 编码 #charset utf-8; # 虚拟主机日志文件 #access_log logs/host.access.log main; location / { root html; index index.html index.htm; } } 3. 防盗链配置 server { # 监听端口 listen 80; # 域名,可以有多个,用空格隔开 server_name localhost; #对源站点验证 valid_referers *.aqiang.com; # 如果不是在该域名下的非法引入会进入下方判断 if ($invalid_referer) { return 404; } # 编码 #charset utf-8; # 虚拟主机日志文件 #access_log logs/host.access.log main; location / { root html; index index.html index.htm; } } 4. 负载均衡配置 upstream myserver { server http://192.168.204.2:8080; server http://192.168.204.3:8080; } server { .... location / { #请求转向mysvr 定义的服务器列表 proxy_pass myserver; proxy_redirect off; proxy_set_header X-Real-IP $remote_addr; #后端的Web服务器可以通过X-Forwarded-For获取用户真实IP proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; } }1、热备:如果你有2台服务器,当一台服务器发生事故时,才启用第二台服务器给提供服务。 upstream myserver { server http://192.168.204.2:8080; server http://192.168.204.3:8080; backup; #热备 }2、轮询:nginx默认权重就是轮询,都默认为1,请求顺序12121212... upstream myserver { server http://192.168.204.2:8080; server http://192.168.204.3:8080; }3、加权轮询:跟据配置的权重的大小而分发给不同服务器不同数量的请求。如果不设置,则默认为1。请求顺序为122122122... upstream myserver { server http://192.168.204.2:8080 weight=1; server http://192.168.204.3:8080 weight=2; }4、ip_hash会让相同的客户端ip请求相同的服务器。 upstream myserver { server http://192.168.204.2:8080; server http://192.168.204.3:8080; ip_hash; }5、 负载均衡 url_hash,相同的URI请求到相同的服务器中 upstream myserver { # url hash hash $request_uri; server http://192.168.204.2:8080; server http://192.168.204.3:8080; }6、 负载均衡 least_conn ,请求走到最少连接的服务器中 upstream myserver { # 最少连接数 least_conn server http://192.168.204.2:8080; server http://192.168.204.3:8080; }7、状态参数解析 upstream myserver { server http://192.168.204.2:8080 weight=2 max_fails=2 fail_timeout=2; server http://192.168.204.3:8080 weight=1 max_fails=2 fail_timeout=1; } down,表示当前的server暂时不参与负载均衡。 backup,预留的备份机器。当其他所有的非backup机器出现故障或者忙的时候,才会请求backup机器,因此这台机器的压力最轻。 max_fails,允许请求失败的次数,默认为1。当超过最大次数时,标识当前服务器不可用,是返回proxy_next_upstream 模块定义的错误。 fail_timeout,在经历了max_fails次失败后,暂停服务的时间,默认是10s。max_fails可以和fail_timeout一起使用。 weight 默认为1.weight越大,负载的权重就越大。 max_conns,服务的最大连接数,默认=0,不进行限制 slow_start,让服务慢慢加入到集群,server是权重类型的。 5. Keepalived 提高吞吐量 upstream myserver { server http://192.168.204.2:8080; server http://192.168.204.3:8080; keepalive 32; } server { .... location / { proxy_pass myserver; proxy_http_version 1.1; proxy_set_header Connection ""; } }keepalived : 设置长连接处理的数量 proxy_http_version :设置长连接http版本为1.1 proxy_set_header :清除connection header 信息 6. 配置HTTPS1、安装SSL模块 ./configure --原来有的模块(如果有的话) --with-http_ssl_module2、下载SSL证书到服务器中 将已获取到的 my.crt 证书文件和 my.key 私钥文件从本地目录拷贝到 Nginx 服务器的 /usr/local/nginx/conf 目录(此处为 Nginx 默认安装目录,请根据实际情况操作)下。 3、配置SSL server { #SSL 默认访问端口号为 443 listen 443 ssl; #请填写绑定证书的域名 server_name cloud.tencent.com; #请填写证书文件的相对路径或绝对路径 ssl_certificate my.crt; #请填写私钥文件的相对路径或绝对路径 ssl_certificate_key my.key; ssl_session_timeout 5m; #请按照以下协议配置 ssl_protocols TLSv1.2 TLSv1.3; #请按照以下套件配置,配置加密套件,写法遵循 openssl 标准。 ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:HIGH:!aNULL:!MD5:!RC4:!DHE; ssl_prefer_server_ciphers on; location / { #网站主页路径。此路径仅供参考,具体请您按照实际目录操作。 #例如,您的网站主页在 Nginx 服务器的 /etc/www 目录下,则请修改 root 后面的 html 为 /etc/www。 root html; index index.html index.htm; } }参考:腾讯云:Nginx 服务器 SSL 证书安装部署 |
今日新闻 |
推荐新闻 |
CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3 |