我使用 Nginx 作为 Apache 的反向代理,但所有网站加载时间都很长。而且我觉得 Nginx 的工作效果不如预期。
我确定虚拟主机配置有问题。以下是我使用的虚拟主机配置:
server {
root /home/username/public_html;
listen xx.xx.xx.xx:80;
server_name domain.com www.domain.com;
access_log /dev/null;
error_log /usr/local/nginx/logs/vhost-error_log warn;
index index.php index.html index.htm;
location / {
location ~.*.(3gp|gif|jpg|jpeg|png|ico|wmv|avi|asf|asx|mpg|mpeg|mp4|pls|mp3|mid|wav|swf|flv|txt|js|css|exe|zip|tar|rar|gz|tgz|bz2|uha|7z|doc|docx|xls|xlsx|pdf|iso|woff|ttf|svg|eot)$ {
gzip_static on;
proxy_cache nginx-cache;
root /home/username/public_html;
tcp_nodelay On;
tcp_nopush On;
access_log /dev/null;
log_not_found Off;
expires 7d;
}
error_page 404 403 502 505 506 = @apachebackend;
add_header X-Cache "HIT from Backend";
proxy_pass http://xx.xx.xx.xx:8080;
include /usr/local/nginx/conf/proxy.conf;
}
location @apachebackend {
internal;
proxy_pass http://xx.xx.xx.xx:8080;
include /usr/local/nginx/conf/proxy.conf;
}
}
以下还包含以下内容proxy.conf
:
proxy_buffering Off;
proxy_cache_valid 404 3h;
proxy_cache_valid 500 502 504 406 3h;
proxy_cache_valid 200 6h;
proxy_buffers 100 128k;
proxy_busy_buffers_size 512k;
proxy_buffer_size 128k;
proxy_cache_use_stale error timeout invalid_header updating http_500 http_502 http_503 http_504;
proxy_cache nginx-cache;
proxy_cache_key "$host$request_uri";
proxy_ignore_headers Set-Cookie;
proxy_cache_min_uses 3;
proxy_max_temp_file_size 0;
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 50m;
client_body_buffer_size 15m;
proxy_connect_timeout 300s;
proxy_read_timeout 300s;
proxy_send_timeout 300s;
proxy_ignore_client_abort off;
proxy_intercept_errors off;
proxy_cache_bypass $cookie_nocache $arg_nocache $arg_comment;
proxy_cache_bypass $http_pragma $http_authorization;
任何帮助将不胜感激。
答案1
尝试执行以下操作:删除proxy_cache nginx-cache;
,移除proxy_buffering Off;
并proxy.conf
用以下值替换内容:
proxy_buffers 32 4m;
proxy_busy_buffers_size 25m;
proxy_buffer_size 512k;
proxy_ignore_headers "Cache-Control" "Expires";
proxy_max_temp_file_size 0;
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 1024m;
client_body_buffer_size 4m;
proxy_connect_timeout 300;
proxy_read_timeout 300;
proxy_send_timeout 300;
proxy_intercept_errors off;