当前位置 主页 > 服务器问题 > nginx问题汇总 >

    nginx实现负载均衡和动静分离

    栏目:nginx问题汇总 时间:2019-03-09 16:05

    这篇文章主要为大家详细介绍了nginx实现负载均衡和动静分离,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

    nginx配置(windows配置),供大家参考,具体内容如下

    以下是我的项目用到的一份配置文件

    #user nobody;worker_processes 4; #进程数,一般cpu是几核就写多少#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; proxy_connect_timeout 15s;  proxy_send_timeout 15s;  proxy_read_timeout 15s; fastcgi_buffers 8 128k; gzip on; client_max_body_size 30m; gzip_min_length 1k; gzip_buffers 16 64k; gzip_http_version 1.1; gzip_comp_level 6; gzip_types text/plain application/x-javascript text/css application/xml application/javascript image/jpeg image/gif image/png image/webp; gzip_vary on; #第一个集群 upstream xdx.com{ server 119.10.52.28:8081 weight=100; server 119.10.52.28:8082 weight=100;  } #第二个集群,用于上传图片所用 upstream xdxfile.com{ server 119.10.52.28:8081;#关于文件上传的请求均访问这个集群 } #第三个集群 upstream xdx8082.com{ server 119.10.52.28:8082;#8082 } #第四个集群 upstream xdxali.com{ server 139.196.235.228:8082;#阿里云 } #第五个集群 upstream xdxaliws.com{ server 139.196.235.228:8886;#阿里云websocket }#第一个代理服务器,监听的是80端口,监听的域名是www.wonyen.com或者wonyen.com server { listen 80;#监听的端口 server_name www.wonyen.com wonyen.com;#监听的域名 #charset koi8-r; #access_log logs/host.access.log main; #location指的是访问的路径,下面这条配置表示当访问网站的根目录,即访问wonyen.com或者www.wonyen.com的时候,就去根目录为html的下面去寻找index.html或者index.htm。在index.html这个页面里面你可以做一些重定向的工作,跳转到指定页面 #也可以自定义到某个集群 # location / {  # root html;  # index index.html index.htm; #} #所有静态请求都交由nginx处理,存放目录为webapps下的root,过期时间为30天  location ~ \.(css|js|gif|jpg|jpeg|png|bmp|swf|eot|svg|ttf|woff|mp3|mp4|wav|wmv|flv|f4v|json)$ {   root apache-tomcat-8.0.9-windows-x86-yipin-8081/apache-tomcat-8.0.9/webapps/ROOT;   expires 30d;  } #配置以Att结尾的请求的处理集群为http://xdxfile.com location ~ ^/\w+Att{  proxy_pass http://xdxfile.com; } #配置以Fill结尾的请求的处理集群为http://xdxfile.com location ~ ^/\w+Fill{  proxy_pass http://xdxfile.com; } #精准配置,如果请求名为/crowdFundSave,则 location = /crowdFundSave{  proxy_pass http://xdxfile.com; } #精确配置,同上 location = /crowdFundRewardSave{  proxy_pass http://xdxfile.com; } #精确配置,同上 location = /garbageCategorySave{  proxy_pass http://xdxfile.com; } #精确配置,同上 location = /mailTestAjax{  proxy_pass http://xdx8082.com; } #精确配置,同上 location = /mailSendAjax{  proxy_pass http://xdx8082.com; } #精确配置,同上 location = /mailOldAjax{  proxy_pass http://xdx8082.com; } #精确配置,同上 #location = /wechatAuthority{  #proxy_pass http://xdxali.com; #} location ~ ^/ueditor1_4_3{  proxy_pass http://xdxfile.com; } #其他所有请求都访问 http://xdx.com的集群   location ~ .*$ {  index index;  proxy_pass http://xdx.com;  } #404页面访问/Error404.jsp这个location error_page 404  /Error404.jsp; #500等页面也访问 /Error404.jsp这个location error_page 500 502 503 504 /Error404.jsp; #配置请求/Error404.jsp就访问http://xdxfile.com集群 location = /Error404.jsp {  proxy_pass http://xdxfile.com; } # 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 #另外一个代理服务器,监听8886接口,监听的域名为www.wonyen.com或者wonyen.com server { listen 8886; server_name www.wonyen.com wonyen.com;#配置若请求为wonyen.com:8086(根目录),就让他去访问http://xdxaliws.com这个集群,这边配置的是websocket的服务端 location / {  proxy_pass http://xdxaliws.com;  proxy_http_version 1.1;  proxy_set_header Upgrade $http_upgrade;  proxy_set_header Connection "upgrade"; } } # 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; # } #}}