Nginx动静分离+缓存

  • 来源:
  • 更新日期:2018-05-14

摘要:#进入Nginx应用目录  cd /usr/local/nginx/conf  #备份原nginx.conf文件  mv  nginx.conf  nginx.bak   创建 vi nginx.conf ,并写入如下内容: user www www;  worker_processes 8;  worker_cpu_affinity 00000001 00000010 00000100 00

#进入Nginx应用目录 cd/usr/local/nginx/conf #备份原nginx.conf文件 mvnginx.confnginx.bak 创建 vi nginx.conf ,

 

002UASMrzy7605pjKJv15&690.jpg并写入如下内容: userwwwwww; worker_processes8; worker_cpu_affinity0000000100000010000001000000100000010000001000000100000010000000; pid/usr/local/nginx/nginx.pid; worker_rlimit_nofile102400; events { useepoll; worker_connections102400; } http { includemime.types; default_typeapplication/octet-stream; fastcgi_intercept_errorson; charsetutf-8; server_names_hash_bucket_size128; client_header_buffer_size4k; large_client_header_buffers432k; client_max_body_size300m; sendfileon; tcp_nopushon; keepalive_timeout60; tcp_nodelayon; client_body_buffer_size512k; #该指令设置与upstream server的连接超时时间,有必要记住,这个超时不能超过75秒 proxy_connect_timeout5; proxy_read_timeout60; proxy_send_timeout5; proxy_buffer_size16k; proxy_buffers464k; proxy_busy_buffers_size128k; proxy_temp_file_write_size128k; #压缩设置 gzipon; gzip_min_length1k; gzip_buffers416k; gzip_http_version1.1; gzip_comp_level2; gzip_typestext/plainapplication/x-javascripttext/cssapplication/xml; gzip_varyon; ###2012-12-19changenginxlogs log_formatmain\'$http_x_forwarded_for-$remote_user[$time_local]"$request"\' \'$status$body_bytes_sent"$http_referer"\' \'"$http_user_agent"$request_time$remote_addr\'; #这里为后端服务器wugk应用集群配置,根据后端实际情况修改即可,tdt_wugk为负载均衡名称,可以任意指定 #但必须跟vhosts.conf虚拟主机的pass段一致,否则不能转发后端的请求。 upstreamtdt_wugk{ server10.10.141.30:8080weight=1max_fails=2fail_timeout=30s; server10.10.141.30:8081weight=1max_fails=2fail_timeout=30s; server10.10.141.31:8080weight=1max_fails=2fail_timeout=30s; server10.10.141.31:8081weight=1max_fails=2fail_timeout=30s; server10.10.141.32:8080weight=1max_fails=2fail_timeout=30s; server10.10.141.32:8081weight=1max_fails=2fail_timeout=30s; } #这里为后端APP应用负载均衡配置,根据后端实际情况修改即可。tdt_app为负载均衡名称,可以任意指定 upstreamtdt_app{ server10.10.141.40:8080weight=1max_fails=2fail_timeout=30s; server10.10.141.40:8081weight=1max_fails=2fail_timeout=30s; server10.10.141.41:8080weight=1max_fails=2fail_timeout=30s; server10.10.141.41:8081weight=1max_fails=2fail_timeout=30s; server10.10.141.42:8080weight=1max_fails=2fail_timeout=30s; server10.10.141.42:8081weight=1max_fails=2fail_timeout=30s; } #include引用vhosts.conf,该文件主要用于配置Nginx 虚拟主机 includevhosts.conf; } 如上nginx.conf配置完毕,继续配置nginx虚拟主机,继续在当前目录创建vhosts.conf vi vhosts.conf 内容如下: ####www.wuguangke.cn server { listen80; server_namewww.wuguangke.cn; indexindex.htmlindex.htm; #配置发布目录为/data/www/wugk root/data/www/wugk; location/ { proxy_next_upstreamhttp_502http_504errortimeoutinvalid_header; proxy_set_headerHost$host; proxy_set_headerX-Real-IP$remote_addr; proxy_set_headerX-Forwarded-For$proxy_add_x_forwarded_for; proxy_passhttp://tdt_wugk; expires3d; } #动态页面交给http://tdt_wugk,也即我们之前在nginx.conf定义的upstreamtdt_wugk均衡 location~.*.(php|jsp|cgi)?$ { proxy_set_headerHost$host; proxy_set_headerX-Real-IP$remote_addr; proxy_set_headerX-Forwarded-For$proxy_add_x_forwarded_for; proxy_passhttp://tdt_wugk; } #配置Nginx动静分离,定义的静态页面直接从Nginx发布目录读取。 location~.*.(html|htm|gif|jpg|jpeg|bmp|png|ico|txt|js|css)$ { root/data/www/wugk; #expires定义用户浏览器缓存的时间为3天,如果静态页面不常更新,可以设置更长,这样可以节省带宽和缓解服务器的压力 expires3d; } #定义Nginx输出日志的路径 access_log/data/logs/nginx_wugk/access.logmain; error_log/data/logs/nginx_wugk/error.logcrit; } ##########chinaapp.sinaapp.com2012-12-19 server { listen80; server_namechinaapp.sinaapp.com; indexindex.htmlindex.htm; root/data/www; location/ { proxy_next_upstreamhttp_502http_504errortimeoutinvalid_header; proxy_set_headerHost$host; proxy_set_headerX-Real-IP$remote_addr; proxy_set_headerX-Forwarded-For$proxy_add_x_forwarded_for; proxy_passhttp://tdt_app;#反向代理(app) expires3d; } #动态网站跳转 location~.*.(php|jsp|cgi)?$ { proxy_set_headerHost$host; proxy_set_headerX-Real-IP$remote_addr; proxy_set_headerX-Forwarded-For$proxy_add_x_forwarded_for; proxy_passhttp://tdt_app; } #静态的文件(图片、js、CSS等) location~.*.(html|htm|gif|jpg|jpeg|bmp|png|ico|txt|js|css)$ { root/data/www/app; expires3d; } #定义Nginx输出日志的路径 access_log/data/logs/nginx_app/access.logmain; error_log/data/logs/nginx_app/error.logcrit; }