Nginx的反向代理,负载均衡配置
2018-08-28 18:59
197 查看
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_39403545/article/details/82152620
直接上代码
nginx.conf 文件
[code] #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 { include 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 logs/access.log main; sendfile on; #tcp_nopush on; #keepalive_timeout 0; keepalive_timeout 65; #gzip on; upstream myapp { server 192.168.194.128:8081 weight=1 max_fails=2 fail_timeout=30s; server 192.168.194.128:8082 weight=2 max_fails=2 fail_timeout=30s; } server { listen 80; server_name localhost; #charset koi8-r; #access_log logs/host.access.log main; location / { proxy_pass http://myapp; 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 # #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 { # 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; # } #} }
解析
如图:
①反向代理:在server { }里的location / { }中配置一个 proxy_pass http://xxx,把请求转发给这个代理来处理
②负载均衡:在server { }外面配置一个 upstream xxx { },里面是服务器的配置,通过设置权重weight的值,就可以实现负载均衡,权重大的访问次数多
阅读更多相关文章推荐
- 在ubuntu上面配置nginx实现反向代理和负载均衡
- 读《实战nginx-取代Apache的高性能web服务器》-"Nginx HTTP 负载均衡和反向代理的配置与优化"精华纪要
- nginx服务器图片的反向代理和负载均衡配置
- nginx的配置、虚拟主机、负载均衡和反向代理(1)
- linux CentOS7 下 Nginx 1.13.7 配置反向代理与负载均衡
- nginx的配置、虚拟主机、负载均衡和反向代理
- Nginx实现反向代理和负载均衡的配置及优化
- 基于nginx和tengine的tcp反向代理,负载均衡 安装和配置
- nginx的反向代理和负载均衡配置方法
- nginx的配置、虚拟主机、负载均衡和反向代理(1)
- 详解Nginx HTTP负载均衡和反向代理配置
- nginx 服务器的反向代理和负载均衡配置
- nginx之反向代理、负载均衡、http服务器、动静分离的配置
- nginx的配置、虚拟主机、负载均衡和反向代理
- Nginx学习笔记(三)--- Nginx实现反向代理和配置负载均衡
- 学习Nginx HTTP负载均衡和反向代理的配置与优化(二)
- nginx的配置、虚拟主机、负载均衡和反向代理--01
- Nginx反向代理以及负载均衡配置
- Nginx 负载均衡和反向代理配置和优化
- nginx的配置、虚拟主机、负载均衡和反向代理(2)