Skip to content

Commit 2af7bfa

Browse files
author
Anton Sannikov
committed
Varnish vs Nginx configuration added
1 parent babfef7 commit 2af7bfa

9 files changed

Lines changed: 454 additions & 17 deletions

File tree

DOCUMENTATION/content/documentation/index.md

Lines changed: 73 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -674,6 +674,7 @@ You may wanna change the default security configuration, so go to `http://localh
674674

675675
<br>
676676
<a name="Use-Redis"></a>
677+
677678
## Use Redis
678679

679680
1 - First make sure you run the Redis Container (`redis`) with the `docker-compose up` command.
@@ -761,12 +762,81 @@ Read the [Laravel official documentation](https://laravel.com/docs/5.7/redis#con
761762
```
762763

763764

764-
765-
766-
765+
<br>
766+
<a name="Use-Varnish"></a>
767+
768+
## Use Varnish
769+
770+
The goal was to proxy request to varnish server using nginx. So only nginx has been configured for Varnish proxy.
771+
Nginx is on port 80 or 443. Nginx sends request through varnish server and varnish server sends request back to nginx on port 81 (external port is defined in `VARNISH_BACKEND_PORT`).
772+
773+
The idea was taken from this [post](https://www.linode.com/docs/websites/varnish/use-varnish-and-nginx-to-serve-wordpress-over-ssl-and-http-on-debian-8/)
774+
775+
The Varnish configuration was developed and tested for Wordpress only. Probably it works with other systems.
776+
777+
#### Steps to configure varnish proxy server:
778+
1. You have to set domain name for VARNISH_PROXY1_BACKEND_HOST variable.
779+
2. If you want to use varnish for different domains, you have to add new configuration section in your env file.
780+
```
781+
VARNISH_PROXY1_CACHE_SIZE=128m
782+
VARNISH_PROXY1_BACKEND_HOST=replace_with_your_domain.name
783+
VARNISH_PROXY1_SERVER=SERVER1
784+
```
785+
3. Then you have to add new config section into docker-compose.yml with related variables:
786+
```
787+
custom_proxy_name:
788+
container_name: custom_proxy_name
789+
build: ./varnish
790+
expose:
791+
- ${VARNISH_PORT}
792+
environment:
793+
- VARNISH_CONFIG=${VARNISH_CONFIG}
794+
- CACHE_SIZE=${VARNISH_PROXY2_CACHE_SIZE}
795+
- VARNISHD_PARAMS=${VARNISHD_PARAMS}
796+
- VARNISH_PORT=${VARNISH_PORT}
797+
- BACKEND_HOST=${VARNISH_PROXY2_BACKEND_HOST}
798+
- BACKEND_PORT=${VARNISH_BACKEND_PORT}
799+
- VARNISH_SERVER=${VARNISH_PROXY2_SERVER}
800+
ports:
801+
- "${VARNISH_PORT}:${VARNISH_PORT}"
802+
links:
803+
- workspace
804+
networks:
805+
- frontend
806+
```
807+
4. change your varnish config and add nginx configuration. Example Nginx configuration is here: `nginx/sites/laravel_varnish.conf.example`.
808+
5. `varnish/default.vcl` is old varnish configuration, which was used in the previous version. Use `default_wordpress.vcl` instead.
809+
810+
#### How to run:
811+
1. Rename `default_wordpress.vcl` to `default.vcl`
812+
2. `docker-compose up -d nginx`
813+
3. `docker-compose up -d proxy`
814+
815+
Keep in mind that varnish server must be built after Nginx cause varnish checks domain affordability.
816+
817+
#### FAQ:
818+
819+
1. How to purge cache? <br>
820+
run from any cli: <br>`curl -X PURGE https://yourwebsite.com/`.
821+
2. How to reload varnish?<br>
822+
`docker container exec proxy varnishreload`
823+
3. Which varnish commands are allowed?
824+
- varnishadm
825+
- varnishd
826+
- varnishhist
827+
- varnishlog
828+
- varnishncsa
829+
- varnishreload
830+
- varnishstat
831+
- varnishtest
832+
- varnishtop
833+
4. How to reload Nginx?<br>
834+
`docker exec Nginx nginx -t`<br>
835+
`docker exec Nginx nginx -s reload`
767836
768837
<br>
769838
<a name="Use-Mongo"></a>
839+
770840
## Use Mongo
771841
772842
1 - First install `mongo` in the Workspace and the PHP-FPM Containers:

docker-compose.yml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -291,6 +291,7 @@ services:
291291
ports:
292292
- "${NGINX_HOST_HTTP_PORT}:80"
293293
- "${NGINX_HOST_HTTPS_PORT}:443"
294+
- "${VARNISH_BACKEND_PORT}:81"
294295
depends_on:
295296
- php-fpm
296297
networks:
@@ -836,6 +837,7 @@ services:
836837

837838
### Varnish ##########################################
838839
proxy:
840+
container_name: proxy
839841
build: ./varnish
840842
expose:
841843
- ${VARNISH_PORT}
@@ -847,12 +849,15 @@ services:
847849
- BACKEND_HOST=${VARNISH_PROXY1_BACKEND_HOST}
848850
- BACKEND_PORT=${VARNISH_BACKEND_PORT}
849851
- VARNISH_SERVER=${VARNISH_PROXY1_SERVER}
852+
ports:
853+
- "${VARNISH_PORT}:${VARNISH_PORT}"
850854
links:
851855
- workspace
852856
networks:
853857
- frontend
854858

855859
proxy2:
860+
container_name: proxy2
856861
build: ./varnish
857862
expose:
858863
- ${VARNISH_PORT}
@@ -864,6 +869,8 @@ services:
864869
- BACKEND_HOST=${VARNISH_PROXY2_BACKEND_HOST}
865870
- BACKEND_PORT=${VARNISH_BACKEND_PORT}
866871
- VARNISH_SERVER=${VARNISH_PROXY2_SERVER}
872+
ports:
873+
- "${VARNISH_PORT}:${VARNISH_PORT}"
867874
links:
868875
- workspace
869876
networks:

env-example

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -364,8 +364,8 @@ MAILDEV_SMTP_PORT=25
364364
### VARNISH ###############################################
365365

366366
VARNISH_CONFIG=/etc/varnish/default.vcl
367-
VARNISH_PORT=8080
368-
VARNISH_BACKEND_PORT=8888
367+
VARNISH_PORT=6081
368+
VARNISH_BACKEND_PORT=81
369369
VARNISHD_PARAMS=-p default_ttl=3600 -p default_grace=3600
370370

371371
### Varnish ###############################################

nginx/Dockerfile

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ RUN apk update \
1818
&& apk add --no-cache openssl \
1919
&& apk add --no-cache bash
2020

21+
RUN apk add --no-cache curl
22+
2123
RUN set -x ; \
2224
addgroup -g 82 -S www-data ; \
2325
adduser -u 82 -D -S -G www-data www-data && exit 0 ; exit 1
@@ -39,4 +41,4 @@ ADD ./startup.sh /opt/startup.sh
3941
RUN sed -i 's/\r//g' /opt/startup.sh
4042
CMD ["/bin/bash", "/opt/startup.sh"]
4143

42-
EXPOSE 80 443
44+
EXPOSE 80 81 443
Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
server {
2+
listen 80;
3+
listen [::]:80;
4+
server_name www.laravel.test;
5+
rewrite ^(.*) https://laravel.test$1/ permanent;
6+
}
7+
8+
server {
9+
listen 80;
10+
listen [::]:80;
11+
server_name laravel.test;
12+
rewrite ^(.*) https://laravel.test$1/ permanent;
13+
}
14+
15+
server {
16+
listen 443 ssl ;
17+
listen [::]:443 ssl;
18+
ssl_certificate /etc/nginx/ssl/laravel.test.crt;
19+
ssl_certificate_key /etc/nginx/ssl/laravel.test.key;
20+
server_name www.laravel.test;
21+
rewrite ^(.*) https://laravel.test$1/ permanent;
22+
}
23+
24+
server {
25+
server_name laravel.test;
26+
27+
# For https
28+
listen 443 ssl ;
29+
listen [::]:443 ssl;
30+
ssl_certificate /etc/nginx/ssl/laravel.test.crt;
31+
ssl_certificate_key /etc/nginx/ssl/laravel.test.key;
32+
33+
port_in_redirect off;
34+
35+
add_header Strict-Transport-Security "max-age=31536000";
36+
add_header X-Content-Type-Options nosniff;
37+
38+
location / {
39+
proxy_pass http://proxy:6081;
40+
proxy_set_header Host $http_host;
41+
proxy_set_header X-Forwarded-Host $http_host;
42+
proxy_set_header X-Real-IP $remote_addr;
43+
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
44+
proxy_set_header X-Forwarded-Proto https;
45+
proxy_set_header HTTPS "on";
46+
proxy_redirect off;
47+
}
48+
}
49+
50+
server {
51+
server_name laravel.test;
52+
53+
listen 81;
54+
listen [::]:81;
55+
56+
root /var/www/laravel.test/www;
57+
58+
index index.php index.html index.htm;
59+
60+
location / {
61+
try_files $uri $uri/ /index.php$is_args$args;
62+
}
63+
64+
location ~ \.php$ {
65+
fastcgi_max_temp_file_size 4m;
66+
fastcgi_pass php-upstream;
67+
68+
# Additional configs
69+
fastcgi_pass_header Set-Cookie;
70+
fastcgi_pass_header Cookie;
71+
fastcgi_ignore_headers Cache-Control Expires Set-Cookie;
72+
try_files $uri /index.php =404;
73+
fastcgi_index index.php;
74+
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
75+
fastcgi_split_path_info ^(.+\.php)(/.+)$;
76+
fastcgi_param PATH_INFO $fastcgi_path_info;
77+
fastcgi_param PATH_TRANSLATED $document_root$fastcgi_path_info;
78+
fastcgi_param HTTPS on;
79+
80+
fastcgi_buffers 16 16k;
81+
fastcgi_buffer_size 32k;
82+
83+
fastcgi_intercept_errors on;
84+
85+
#fixes timeouts
86+
fastcgi_read_timeout 600;
87+
include fastcgi_params;
88+
}
89+
90+
# Caching
91+
location ~* \.(ico|jpg|webp|jpeg|gif|css|png|js|ico|bmp|zip|woff)$ {
92+
access_log off;
93+
log_not_found off;
94+
add_header Pragma public;
95+
add_header Cache-Control "public";
96+
expires 14d;
97+
}
98+
99+
location ~* \.(php|html)$ {
100+
access_log on;
101+
log_not_found on;
102+
add_header Pragma public;
103+
add_header Cache-Control "public";
104+
expires 14d;
105+
}
106+
107+
location ~ /\.ht {
108+
deny all;
109+
}
110+
}

varnish/Dockerfile

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,8 @@
1-
FROM debian:latest
2-
3-
LABEL maintainer="ZeroC0D3 Team<zeroc0d3.team@gmail.com>"
1+
FROM varnish:6.3
42

53
# Set Environment Variables
64
ENV DEBIAN_FRONTEND noninteractive
75

8-
# Install Dependencies
9-
RUN apt-get update && apt-get install -y apt-utils && apt-get upgrade -y
10-
RUN mkdir /home/site && mkdir /home/site/cache
11-
RUN apt-get install -y varnish
12-
RUN rm -rf /var/lib/apt/lists/*
13-
146
# Setting Configurations
157
ENV VARNISH_CONFIG /etc/varnish/default.vcl
168
ENV CACHE_SIZE 128m

varnish/default.vcl

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ sub vcl_init {
5252
# vdir.add_backend(servern);
5353
}
5454

55+
# This function is used when a request is send by a HTTP client (Browser)
5556
sub vcl_recv {
5657
# Called at the beginning of a request, after the complete request has been received and parsed.
5758
# Its purpose is to decide whether or not to serve the request, how to do it, and, if applicable,
@@ -75,8 +76,12 @@ sub vcl_recv {
7576
# Not from an allowed IP? Then die with an error.
7677
return (synth(405, "This IP is not allowed to send PURGE requests."));
7778
}
78-
# If you got this stage (and didn't error out above), purge the cached result
79-
return (purge);
79+
80+
ban("req.http.host == " + req.http.host);
81+
# Throw a synthetic page so the request won't go to the backend.
82+
return(synth(200, "Ban added"));
83+
# If allowed, do a cache_lookup -> vlc_hit() or vlc_miss()
84+
#return (purge);
8085
}
8186

8287
# Only deal with "normal" types

0 commit comments

Comments
 (0)