10

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.

techraf
  • 4,163
  • 8
  • 27
  • 44
HuMaN-BiEnG
  • 103
  • 1
  • 1
  • 6
  • 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 – HuMaN-BiEnG 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 http://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 – HuMaN-BiEnG 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
  • Aha i see man, but now how can i reduce page load time, i'm sure that something is wrong there with the configuration provided – HuMaN-BiEnG Aug 14 '16 at 12:32
  • Try to comment all and keep only the basic minimum required configurations for reverse proxy to work. In my case, it was the issue of `proxy_set_header Host`. Can you try using host value manually instead of using `$host`? – Sohail Ahmed Aug 14 '16 at 14:25

1 Answers1

10

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;
Anubioz
  • 3,597
  • 17
  • 23
  • Thank you so much this settings had reduced load time to 5.9 s, but with out removing gzip_static & proxy cache – HuMaN-BiEnG Aug 14 '16 at 15:08
  • [`gzip_static`](http://nginx.org/en/docs/http/ngx_http_gzip_static_module.html) 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;`](http://nginx.org/en/docs/http/ngx_http_proxy_module.html#proxy_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 :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; – HuMaN-BiEnG Aug 14 '16 at 15:23
  • You should [use memcached](http://nginx.org/ru/docs/http/ngx_http_memcached_module.html) to store cache instead of disk. And be sure to try adding [pagespeed_module to nginx](https://developers.google.com/speed/pagespeed/module/build_ngx_pagespeed_from_source) all you need is to [recompile it from source](http://serverfault.com/questions/795556/supporting-http2-on-amazon-linux-with-apache-with-openssl-1-0-1/795582#795582) will take 5 minutes of your time) - it's way to improve load times even further – Anubioz Aug 14 '16 at 15:31
  • @Anubioz you're very wrong about what `gzip_static` does. It doesn't serve files by `http://example.com/index.html.gz` requests. It serves precompressed version of `index.html` with appropriate `Content-Encoding`. – VBart Aug 15 '16 at 06:45
  • @VBart Serves precompressed version if there is such a version there, right? Which gives you an extra (failed) file system request every time nginx searches for a file that is not there. I mean if you actually do create all those .gz files with some script `gzip_static` may be good, but if you don't have any `.gz` files it is just a wasted search for them every request... I *may* be wrong in reading manual though, please clarify... – Anubioz Aug 15 '16 at 07:36
  • Of course it's useful only if such files exist, but it's not clear from the description. – VBart Aug 15 '16 at 07:59
  • My comment was mostly against your phrase _Nowadays it's useless, since no browser ever request them at all_. – VBart Aug 15 '16 at 08:16
  • @VBart Yes, that was my mistake. While doing more search, I discovered that there is a bug which doesn't allow [pagespeed module to work properly if `gzip_static` is enabled](https://github.com/pagespeed/ngx_pagespeed/issues/1053), so I suggest leaving the default setting (turn it off), since pagespeed does wonderful compression/caching job on itself... – Anubioz Aug 15 '16 at 08:25
  • Anubioz i have tried to install pagespeed module but it prevents many configuration options like gzip & expire directives so i disabled it – HuMaN-BiEnG Aug 15 '16 at 10:24
  • What do you mean by "prevent"? Does the https://developers.google.com/speed/pagespeed/insights/ show improvements with pagespeed_module enabled? – Anubioz Aug 15 '16 at 10:31
  • i dont ever recommend using pagespeed module, as after adding it to nginx most websites doesnt work & throw 503 error, without any meaning error in nginx error log file, which wasted my time for two days, beside that many nginx configuration directives didnt work, after i removed the module websites works very well – HuMaN-BiEnG Aug 15 '16 at 10:40
  • I'm using pagespeed for several years without any problems (it tends to break some buggy js, but it can be easily solved by disabling those filters for specific website). Don't give up on pagespeed module just yet, it was just a miconfiguration issue. Have you set permissions and SELinux for /var/cache/pagespeed? Have a look at https://developers.google.com/speed/pagespeed/module/faq - it describes a way to deal with most common errors... – Anubioz Aug 15 '16 at 10:59