• If you are still using CentOS 7.9, it's time to convert to Alma 8 with the free centos2alma tool by Plesk or Plesk Migrator. Please let us know your experiences or concerns in this thread:
    CentOS2Alma discussion

Plesk 11 + Apache + Nginx + Varnish cache

Protazy

New Pleskian
Plesk 11 + Apache + Nginx + Varnish cache (Solved)

Hi there, I have a small problem with setting up a configuration like Plesk + Apache + NginX + Varnish. I used to run similar configuration but without apache. Now since Plesk 11 started to support nginx I decided to run it the way Plesk provides. I have everything working just fine but Varnish. Is there any way to move nginx to port lets say 8080 so Varnish can serve on 80? for example command similar to this one websrvmng --set-http-port --port=8080 ? I'm looking for a solution where I wouldn't have to edit any config files after adding a new domain.
If someone knows, please share :)
Kind regards
Paaul
 
Last edited:
Hi Paaul,

You can define a desired listen port for Nginx in Plesk default templates for Nginx server:
/usr/local/psa/admin/conf/templates/default/nginx.php
/usr/local/psa/admin/conf/templates/default/nginxDomainForwarding.php
/usr/local/psa/admin/conf/templates/default/nginxDomainVhost.php

However, please do not modify these files above. You should copy them into the folder below and there you can adjust the listen port.
/usr/local/psa/admin/conf/templates/custom/

Here is example how the listen port can be changed for Apache:
http://download1.parallels.com/Ples...figuration-guide/index.htm?fileName=64631.htm

P.S.
Why do you need Nginx if you have Varnish?
Are you serving static content with Nginx?
 
Hello Alexey.Plotnitsky, thanks for your replay, will try it asap.
Well I'm hosting a magento store which has a lot of photos. Many, but many people recommend to use nginx for use with magento. I tried it and I have to admit that I noticed a really nice speed leap. Magento support Varnish via extension. If it exists and can speed things up a bit more during rush hours, I don't see any reason why not to put it in front of nginx ;)

Kind regards
Paul
 
Hi there, I have a small problem with setting up a configuration like Plesk + Apache + NginX + Varnish. I used to run similar configuration but without apache. Now since Plesk 11 started to support nginx I decided to run it the way Plesk provides. I have everything working just fine but Varnish. Is there any way to move nginx to port lets say 8080 so Varnish can serve on 80? for example command similar to this one websrvmng --set-http-port --port=8080 ? I'm looking for a solution where I wouldn't have to edit any config files after adding a new domain.
If someone knows, please share :)
Kind regards
Paaul

could you write a small HowTo with Plesk 11 and Apache? Are you running IIS? I want to run Pleask Without IIS on windows Server 2008... Is there any chance to do it?

Regards.
 
No I don't run IIS. It is hard to say for me if it is possible, because I have never worked with Windows servers. I've solved my problem like this: Apache listens on port 7080 by default when Nginx is activated in Plesk. Nginx listens on 80, I used varnish also with default settings (por 6081), but pointed backend to Apache on port 7080. In Nginx configuration file (in templates as well) I changed pass_proxy from 7080 to 6081. And that's it. In this way nginx checks first if content is cached in varnish, if not then goes to apache. In my setup running magento I noticed about 1 sec speed gain when varnish has files already cached. This is what I wanted :) Pages loading in approx. 0.25 sec :)
 
Last edited:
You are the master then :)

Thank you for your reply. I sort out my problem as well... I just get Ubuntu with Plesk for www and I run my game on separate machine :)

Regards.
 
Hello,

I make this of the post, but it doesn´t work by me:
Hi there, I have a small problem with setting up a configuration like Plesk + Apache + NginX + Varnish. I used to run similar configuration but without apache. Now since Plesk 11 started to support nginx I decided to run it the way Plesk provides. I have everything working just fine but Varnish. Is there any way to move nginx to port lets say 8080 so Varnish can serve on 80? for example command similar to this one websrvmng --set-http-port --port=8080 ? I'm looking for a solution where I wouldn't have to edit any config files after adding a new domain.
If someone knows, please share
Kind regards
Paaul

I change only the nginx.php like:

<?php /** @var Template_VariableAccessor $VAR */ ?>
server {
<?php foreach ($VAR->server->ipAddresses->all as $ipAddress): ?>
listen <?php echo "{$ipAddress->escapedAddress}:{$VAR->server->nginx->httpPort}" ?> default_server <?php if ($ipAddress->isIpV6) echo 'ipv6only=on'; else $hasIpV4=true; ?>;
<?php endforeach; ?>
<?php if (!$hasIpV4) echo 'listen 127.0.0.1:' . $VAR->server->nginx->httpPort . ' default_server;'; ?>

location / {
proxy_pass http://127.0.0.1:6081;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
I use Magento on server also and a had follow default vcl:

# This is a basic VCL configuration file for PageCache powered by Varnish for Magento module.

# default backend definition. Set this to point to your content server.
backend admin {
.host = "127.0.0.1";
.port = "7080";
}

# admin backend with longer timeout values. Set this to the same IP & port as your default server.
backend admin {
.host = "127.0.0.1";
.port = "7080";
.first_byte_timeout = 18000s;
.between_bytes_timeout = 18000s;
}

# add your Magento server IP to allow purges from the backend
acl purge {
"localhost";
"127.0.0.1";
}


sub vcl_recv {
if (req.restarts == 0) {
if (req.http.x-forwarded-for) {
set req.http.X-Forwarded-For =
req.http.X-Forwarded-For + ", " + client.ip;
} else {
set req.http.X-Forwarded-For = client.ip;
}
}

if (req.request != "GET" &&
req.request != "HEAD" &&
req.request != "PUT" &&
req.request != "POST" &&
req.request != "TRACE" &&
req.request != "OPTIONS" &&
req.request != "DELETE" &&
req.request != "PURGE") {
/* Non-RFC2616 or CONNECT which is weird. */
return (pipe);
}

# purge request
if (req.request == "PURGE") {
if (!client.ip ~ purge) {
error 405 "Not allowed.";
}
ban("obj.http.X-Purge-Host ~ " + req.http.X-Purge-Host + " && obj.http.X-Purge-URL ~ " + req.http.X-Purge-Regex + " && obj.http.Content-Type ~ " + req.http.X-Purge-Content-Type);
error 200 "Purged.";
}

# switch to admin backend configuration
if (req.http.cookie ~ "adminhtml=") {
set req.backend = admin;
}

# we only deal with GET and HEAD by default
if (req.request != "GET" && req.request != "HEAD") {
return (pass);
}

# normalize url in case of leading HTTP scheme and domain
set req.url = regsub(req.url, "^http?://[^/]+", "");

# static files are always cacheable. remove SSL flag and cookie
if (req.url ~ "^/(media|js|skin)/.*\.(png|jpg|jpeg|gif|css|js|swf|ico)$") {
unset req.http.Https;
unset req.http.Cookie;
}

# not cacheable by default
if (req.http.Authorization || req.http.Https) {
return (pass);
}

# do not cache any page from
# - index files
# - ...
if (req.url ~ "^/(index)") {
return (pass);
}

# as soon as we have a NO_CACHE cookie pass request
if (req.http.cookie ~ "NO_CACHE=") {
return (pass);
}

# normalize Aceept-Encoding header
# http://varnish.projects.linpro.no/wiki/FAQ/Compression
if (req.http.Accept-Encoding) {
if (req.url ~ "\.(jpg|png|gif|gz|tgz|bz2|tbz|mp3|ogg|swf|flv)$") {
# No point in compressing these
remove req.http.Accept-Encoding;
} elsif (req.http.Accept-Encoding ~ "gzip") {
set req.http.Accept-Encoding = "gzip";
} elsif (req.http.Accept-Encoding ~ "deflate" && req.http.user-agent !~ "MSIE") {
set req.http.Accept-Encoding = "deflate";
} else {
# unkown algorithm
remove req.http.Accept-Encoding;
}
}

# remove Google gclid parameters
set req.url = regsuball(req.url,"\?gclid=[^&]+$",""); # strips when QS = "?gclid=AAA"
set req.url = regsuball(req.url,"\?gclid=[^&]+&","?"); # strips when QS = "?gclid=AAA&foo=bar"
set req.url = regsuball(req.url,"&gclid=[^&]+",""); # strips when QS = "?foo=bar&gclid=AAA" or QS = "?foo=bar&gclid=AAA&bar=baz"

return (lookup);
}

sub vcl_hash {
hash_data(req.url);
if (req.http.host) {
hash_data(req.http.host);
} else {
hash_data(server.ip);
}
if (!(req.url ~ "^/(media|js|skin)/.*\.(png|jpg|jpeg|gif|css|js|swf|ico)$")) {
call design_exception;
}
return (hash);
}
#
# sub vcl_hit {
# return (deliver);
# }
#
# sub vcl_miss {
# return (fetch);
# }

sub vcl_fetch {
if (beresp.status == 500) {
set beresp.saintmode = 10s;
return (restart);
}
set beresp.grace = 5m;

# add ban-lurker tags to object
set beresp.http.X-Purge-URL = req.url;
set beresp.http.X-Purge-Host = req.http.host;

if (beresp.status == 200 || beresp.status == 301 || beresp.status == 404) {
if (beresp.http.Content-Type ~ "text/html" || beresp.http.Content-Type ~ "text/xml") {
if ((beresp.http.Set-Cookie ~ "NO_CACHE=") || (beresp.ttl < 1s)) {
set beresp.ttl = 0s;
return (hit_for_pass);
}

# marker for vcl_deliver to reset Age:
set beresp.http.magicmarker = "1";

# Don't cache cookies
unset beresp.http.set-cookie;
} else {
# set default TTL value for static content
set beresp.ttl = 4h;
}
return (deliver);
}

return (hit_for_pass);
}

sub vcl_deliver {
# debug info
if (resp.http.X-Cache-Debug) {
if (obj.hits > 0) {
set resp.http.X-Cache = "HIT";
set resp.http.X-Cache-Hits = obj.hits;
} else {
set resp.http.X-Cache = "MISS";
}
set resp.http.X-Cache-Expires = resp.http.Expires;
} else {
# remove Varnish/proxy header
remove resp.http.X-Varnish;
remove resp.http.Via;
remove resp.http.Age;
remove resp.http.X-Purge-URL;
remove resp.http.X-Purge-Host;
}

if (resp.http.magicmarker) {
# Remove the magic marker
unset resp.http.magicmarker;

set resp.http.Cache-Control = "no-store, no-cache, must-revalidate, post-check=0, pre-check=0";
set resp.http.Pragma = "no-cache";
set resp.http.Expires = "Mon, 31 Mar 2008 10:00:00 GMT";
set resp.http.Age = "0";
}
}

# sub vcl_error {
# set obj.http.Content-Type = "text/html; charset=utf-8";
# set obj.http.Retry-After = "5";
# synthetic {"
# <?xml version="1.0" encoding="utf-8"?>
# <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
# "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
# <html>
# <head>
# <title>"} + obj.status + " " + obj.response + {"</title>
# </head>
# <body>
# <h1>Error "} + obj.status + " " + obj.response + {"</h1>
# <p>"} + obj.response + {"</p>
# <h3>Guru Meditation:</h3>
# <p>XID: "} + req.xid + {"</p>
# <hr>
# <p>Varnish cache server</p>
# </body>
# </html>
# "};
# return (deliver);
# }
#
# sub vcl_init {
# return (ok);
# }
#
# sub vcl_fini {
# return (ok);
# }
sub design_exception {
}


and follow varnish conf etc/default/varnish

####Magento phonix varnish
DAEMON_OPTS="-a :6081
-T localhost:6082
-f /etc/varnish/default.vcl
-u varnish -g varnish \
-S /etc/varnish/secret
-p thread_pools=4
-p thread_pool_add_delay=2
-p thread_pool_min=200
-p thread_pool_max=4000
-p session_linger=50
-p sess_workspace=262144
-s malloc,10G"
This config don´t work ! Please help

Thanks to all supporter
 
Last edited:
I looked at your config file once again and it seems to be OK. The only thing I can spot is:
backend admin {
.host = "127.0.0.1";
.port = "7080";
}

# admin backend with longer timeout values. Set this to the same IP & port as your default server.
backend admin {
.host = "127.0.0.1";
.port = "7080";
.first_byte_timeout = 18000s;
.between_bytes_timeout = 18000s;
}

You have backend admin here twice. One of them should be default
backend default {
.host = "127.0.0.1";
.port = "7080";
}
or if you want to name it in a different way like frontend, then try this:
backend frontend {
.host = "127.0.0.1";
.port = "7080";
}

and in default.vcl put this code changing your domain right after sub vcl_recv { so it looks like this:

sub vcl_recv {
if (req.http.host ~ "(?i)^(www.)?yourdomain.com$") {
set req.backend = frontend;
}
 
Last edited:
nginx -> varnish -> apache for drupal

@Protazy thanks for the idea ! I managed to get this working for my drupal installation.

@MarkusMarcel i think you have to change all three files (not only nginx.php) :

This is my procedure:

( first copy templates files in a new folder named "custom" inside /usr/local/psa/admin/conf/templates/ per #2 comment)

/usr/local/psa/admin/conf/templates/custom/nginx.php
/usr/local/psa/admin/conf/templates/custom/nginxDomainForwarding.php
/usr/local/psa/admin/conf/templates/custom/nginxDomainVhost.php

then i made these modifications, actually i modified the port varnish listens (in my case this is 6081 for varnish)

for nginx.php
......
location / {
proxy_pass http://127.0.0.1:6081;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
..........

for nginxDomainForwarding.php
..........
<?php echo $VAR->includeTemplate('domain/nginxForwarding.php', array(
'ipAddress' => $ipAddress,
'frontendPort' => $VAR->server->nginx->httpPort,
'backendPort' => '6081',
'defaultIp' => $ipAddress->defaultDomainId == $VAR->domain->id ? true : false,
)) ?>
..............

for nginxDomainVhost.php
..........
array(
'ssl' => false,
'frontendPort' => $VAR->server->nginx->httpPort,
'backendPort' => '6081',
'documentRoot' => $VAR->domain->physicalHosting->httpDir,
'ipAddress' => $ipAddress,
...........

Then i stopped nginx service from INSIDE my plesk panel then again start nginx service (no just restarting). This is to ensure that scripts will run and make the modifications for all virtual servers on my machine.

That's it.

My concerns now is than what is going to happen if varnish crashes. Though this is not a plesk panel issue to discuss here, is there any way for nginx to fall back to apache in the event of a varnish crash ?

Thanks !

EDIT: Just to clarify that this configuration is not as this post's title : Varnish -> nginx -> Apache "Sandwich" but rather is nginx -> Varnish -> apache
 
Last edited:
No gain with nginx in front of Varnish

Alexey.Plotnitsky thanks for this information ! I knew that nginx rocks.
I will try this, but now, here comes your question again:
Why do you need Nginx if you have Varnish?
This is true in my configuration. I have mostly drupal installations in my server. Varnish caches also static files.
My tests show that if i request my pages directly to Varnish (http://mysite:6081/) then i see no gain at all. Its just the same (with direct requests to Varnish i have a gain of 10 to 20 req. per sec. more than with nginx, when i am in the range of 2000 req. per sec.)

So why should i have this "sanwich" with no gain and put Varnish on port 80 with no nginx at all ?

Any ideas ?

I think i have to find another solution keeping nginx for flexibility.
 
Hi,

we use php5.5 with Plesk 12 and in this case the nginx.php is a little different then posted here. Is there any chance to get updatet info with php5.5? Original file below

PHP:
<?php echo AUTOGENERATED_CONFIGS; ?>

<?php /** @var Template_VariableAccessor $VAR */ ?>
<?php /** @var Template_Variable_IpAddress $ipAddress */ ?>

include "<?php echo $VAR->server->nginx->httpConfDir ?>/plesk.conf.d/ip_default$

<?php echo $VAR->includeTemplate('server/nginxVhosts.php', array(
    'ssl' => false,
    'frontendPort' => $VAR->server->nginx->httpPort,
    'backendPort' =>  $VAR->server->nginx->httpPort,
)) ?>

<?php echo $VAR->includeTemplate('server/nginxVhosts.php', array(
    'ssl' => true,
    'frontendPort' => $VAR->server->nginx->httpsPort,
    'backendPort' => $VAR->server->webserver->httpsPort,
)) ?>


<?php /* Next block used for watchdog */ ?>

<?php if (!$VAR->server->ipAddresses->hasIpV4Address): ?>
server {
    listen 127.0.0.1 default_server;
    return 200;
}
<?php endif ?>

best regards http://www.noteboox.de/
 
Last edited:
Back
Top