Nginx reverse proxy is slow

I have Nginx working as a reverse proxy for Apache, but it all websites take long time to load. Also I feel that Nginx doesn't work as expected.

I'm sure that there is something wrong with virtual host configuration. The following is virtual host configuration that I use:

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;
}

}

also the following are the contents of 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;

Any help will be appreciated.


Solution 1:

Try do do the following: remove proxy_cache nginx-cache;, remove proxy_buffering Off; and replace proxy.conf contents with the following values:

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;