8

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.

7
  • Can you please post load times with and without nginx? Just to know what "slow" we are talking about?
    – Anubioz
    Aug 14 '16 at 11:54
  • Hello Anubioz, according to webpagetest.org, the first load time is 9.068s Aug 14 '16 at 12:15
  • And if accessed directly from apache via port 8080? By the way, is there a specific reason you don't use proxy_pass http://127.0.0.1:8080; in your config instead of proxy_pass http://xx.xx.xx.xx:8080;?
    – Anubioz
    Aug 14 '16 at 12:17
  • hello Anubioz, if i directly get access domain using domain.com:808 i get "The connection has timed out" error, so if i changed proxy pass from main server ip to localhost does this will fix the problem ? & what is the difference between using localhost & public ip address ? , also under static files block are there any wrong in configuration ?, thank you so much for your cooperation Aug 14 '16 at 12:22
  • Using 127.0.0.1 is safer (provided both nginx & apache are located on the same server), while it won't solve any problems on its own, it may prevent negative effects from some - like with various NAT issues (i.e. having server behind NAT and trying to access it with external IP, while being in internal network)
    – Anubioz
    Aug 14 '16 at 12:29
9

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;
14
  • Thank you so much this settings had reduced load time to 5.9 s, but with out removing gzip_static & proxy cache Aug 14 '16 at 15:08
  • gzip_static serves compressed pages with .gz extension, like in http://example.com/index.html.gz. Nowadays it's useless, since no browser ever request them at all. proxy_cache nginx-cache; requires a memory zone to be defined, and I haven't seen a proxy_cache_path directive in your config, besides automated memory management is pretty good in nginx, I don't see any reason to define that zone manually.
    – Anubioz
    Aug 14 '16 at 15:14
  • It would be nice if you could pinpoint the exact configuration directive that improved performance, so I can improve my answer which now is basically excerpt from my own config...
    – Anubioz
    Aug 14 '16 at 15:17
  • i expect that increasing buffers beside adding proxy_ignore_headers "Cache-Control" "Expires";, improve speed very much, about proxy nginx cache i have it already configured in nginx.conf as shown :<code>proxy_cache_path /home/nginx_cache levels=1:2 keys_zone=nginx-cache:1m max_size=4g inactive=6h; open_file_cache max=3000 inactive=6h; open_file_cache_valid 6h; open_file_cache_min_uses 3; open_file_cache_errors Off;</code> Aug 14 '16 at 15:23
  • You should use memcached to store cache instead of disk. And be sure to try adding pagespeed_module to nginx all you need is to recompile it from source will take 5 minutes of your time) - it's way to improve load times even further
    – Anubioz
    Aug 14 '16 at 15:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.