Skip to content

Commit 1495055

Browse files
Findus23mattab
authored andcommitted
modernize nginx config (#43)
* [WIP] modernize nginx config * update to PHP 7.2 * add Referrer-Policy * fix typos in README * only allow static files from the plugin directory similar to the .htaccess file * properly display textfiles in root directory
1 parent 389a670 commit 1495055

28 files changed

+129
-1282
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
/*tar*
2+
.idea/

README.md

Lines changed: 18 additions & 286 deletions
Original file line numberDiff line numberDiff line change
@@ -1,292 +1,24 @@
1-
# Nginx configuration for Piwik
1+
# Nginx Configuration for Matomo
22

3-
## Introduction
3+
This is a small nginx configuration that should help you get your own Matomo instance running and start collecting your own analytics.
44

5-
This is a nginx configuration for running [Piwik](http://piwik.org "Piwik").
6-
It assumes that the domain assigned to Piwik is `stats.example.com`.
75

8-
Change this according to your server setup.
6+
## I already know nginx
97

10-
## Features
8+
In this case it should be enough to just take the `sites-available/matomo.conf`, check if everything is configured as you like it and enable the config.
119

12-
1. Filtering of invalid HTTP `Host` headers.
10+
## I want to get started
1311

14-
2. Filtering of referrer when serving the Piwik JS or any other
15-
static files.
16-
17-
3. Hiding of all text files.
18-
19-
4. Restricted handling of PHP files. Only `index.php` and
20-
`piwik.php` are allowed. All other attempts to run a PHP file
21-
return a 404.
22-
23-
5. IPv6 and IPv4 support.
24-
25-
6. Possibility of using **Apache** as a backend for dealing with
26-
PHP. This means using Nginx as a
27-
[reverse proxy](http://wiki.nginx.org/HttpProxyModule "Nginx
28-
Proxy Module").
29-
30-
7. Static files use the OS [buffer cache](http://wiki.nginx.org/HttpCoreModule#open_file_cache).
31-
32-
8. Caching of most Piwik pages with exceptions for the installation
33-
and administrative tasks.
34-
35-
9. `piwik.php` is cached with a long TTL (2h). Hence faster to
36-
register an access.
37-
38-
10. Inline `robots.txt` that disables all crawling.
39-
40-
## Nginx as a Reverse Proxy: Proxying to Apache for PHP
41-
42-
This applies if you **absolutely need** to use the rather _bad habit_ of
43-
deploying web apps relying on `.htaccess`, or you just want to use
44-
Nginx as a reverse proxy. The config allows you to do so. Note that
45-
this provides some benefits over using only Apache, since Nginx is
46-
much faster than Apache. Furthermore you can use the proxy cache
47-
and/or use Nginx as a load balancer.
48-
49-
## IPv6 and IPv4
50-
51-
The configuration of the example vhosts uses **separate** sockets for
52-
IPv6 and IPv4. This way is simpler for those not (yet) having IPv6
53-
support to disable it by commenting out the
54-
[`listen`](http://nginx.org/en/docs/http/ngx_http_core_module.html#listen)
55-
directive with the `ipv6only=on` parameter.
56-
57-
Note that the IPv6 address uses an IP _stolen_ from the
58-
[IPv6 Wikipedia page](https://en.wikipedia.org/wiki/IPv6). You **must
59-
replace** the indicated address by **your** address.
60-
61-
## Installation
62-
63-
1. Move the old `/etc/nginx` directory to `/etc/nginx.old`.
64-
65-
2. Clone the git repository from GitHub:
66-
67-
`git clone https://github.com/perusio/piwik-nginx.git /etc/nginx`
68-
69-
3. Edit the `sites-available/stats.example.com.conf` configuration
70-
file to suit your needs. Especially replace stats.example.com
71-
with **your** domain.
72-
73-
4. Setup the PHP handling method. It can be:
74-
+ Upstream HTTP server like Apache with mod_php. To use this
75-
method comment out the `include upstream_phpcgi.conf;`
76-
line in `nginx.conf` and uncomment the lines:
77-
78-
include reverse_proxy.conf;
79-
include upstream_phpapache.conf;
80-
81-
Now you must set the proper address and port for your
82-
backend(s) in the `upstream_phpapache.conf`. By default it
83-
assumes the loopback `127.0.0.1` interface on port
84-
`8080`. Adjust accordingly to reflect your setup.
85-
86-
Comment out **all** `fastcgi_pass` directives in
87-
`stats.example.com.conf` Uncomment out all the `proxy_pass`
88-
directives. They have a comment around them, stating these
89-
instructions.
90-
+ FastCGI process using php-cgi. In this case an
91-
[init script](https://github.com/perusio/php-fastcgi-debian-script
92-
"Init script for php-cgi") is
93-
required.
94-
+ [PHP FPM](http://www.php-fpm.org "PHP FPM"), this requires you to
95-
configure your fpm setup, in Debian/Ubuntu this is done in the
96-
`/etc/php5/fpm` directory. This is how the server is configured
97-
out of the box. It uses UNIX sockets. You can use TCP sockets if
98-
you prefer.
99-
100-
Look [here](https://github.com/perusio/php-fpm-example-config) for
101-
an **example configuration** of `php-fpm`.
102-
103-
Check that the socket is properly created and is listening. This
104-
can be done with `netstat`, like this for UNIX sockets:
105-
106-
`netstat --unix -l`
107-
108-
or like this for TCP sockets:
109-
110-
`netstat -t -l`
111-
112-
It should display the PHP CGI socket.
113-
114-
Note that the default socket type is UNIX and the config assumes
115-
it to be listening on `unix:/tmp/php-cgi/php-cgi.socket`, if
116-
using the `php-cgi`, or in `unix:/var/run/php-fpm.sock` using
117-
`php-fpm` and that you should **change** to reflect your setup
118-
by editing `upstream_phpcgi.conf`.
119-
120-
5. Setup the cache for `piwik.php`. It depends if you use either
121-
FastCGI or Apache for processing PHP.
122-
+ **FastCGI**: Create the `/var/cache/nginx/fcgicache` directory
123-
if you're serving PHP with php-fpm or php cgi. This directory
124-
must be owned by the unpriveleged nginx user. In debian it's
125-
`www-data`.
126-
+ **Apache**: Create the `/var/cache/nginx/proxycache` directory
127-
if you're serving PHP with Apache. This directory must be owned
128-
by the unpriveleged nginx user. In debian it's
129-
`www-data`. Comment out all the lines where `fcgi_cache` is
130-
referenced. You must uncomment the line `include
131-
proxy_cache_zone.conf;` on `nginx.conf`.
132-
133-
6. Create the `/etc/nginx/sites-enabled` directory and enable the
134-
virtual host using one of the methods described below.
135-
136-
Note that if you're using the
137-
[nginx_ensite](http://github.com/perusio/nginx_ensite) script
138-
described below it **creates** the `/etc/nginx/sites-enabled`
139-
directory if it doesn't exist the first time you run it for
140-
enabling a site.
141-
142-
7. Reload Nginx:
143-
144-
`/etc/init.d/nginx reload`
145-
146-
8. Check that your site is working using your browser.
147-
148-
9. Remove the `/etc/nginx.old` directory.
149-
150-
10. Done.
151-
152-
## Caching status
153-
154-
You can check if the responses are being cached or not. For the
155-
short term cache check for a `X-Piwik-Cache` header with a
156-
`HIT/EXPIRED/MISS/UPDATING/STALE` value. The same applies to the
157-
long cache: check for a `X-Piwik-Long-Cache` header with a
158-
`HIT/EXPIRED/MISS/UPDATING/STALE` value.
159-
160-
Example:
161-
162-
curl -I stats.example.com/piwik.php
163-
HTTP/1.1 200 OK
164-
Server: nginx
165-
Date: Mon, 02 Jan 2012 13:17:15 GMT
166-
Content-Type: text/html
167-
Connection: keep-alive
168-
Keep-Alive: timeout=10
169-
Vary: Accept-Encoding
170-
Expires: Thu, 01 Jan 1970 00:00:01 GMT
171-
Cache-Control: no-cache
172-
X-Piwik-Long-Cache: MISS
173-
174-
## Acessing the php-fpm status and ping pages
175-
176-
You can get the
177-
[status and ping](http://forum.nginx.org/read.php?3,56426) pages for
178-
the running instance of `php-fpm`. There is a `php_fpm_status.conf`
179-
file with the configuration for both features.
180-
181-
+ the **status page** at `/fpm-status`;
182-
183-
+ the **ping page** at `/ping`.
184-
185-
For obvious reasons access to these pages is restricted to a given set
186-
of IP addresses. In the suggested configuration only from localhost
187-
and non-routable IPs of the 192.168.1.0 network.
188-
189-
The allowed hosts are defined in a geo block in file
190-
`php_fpm_status_allowed_hosts.conf`. You should edit the predefined IP
191-
addresses to suit your setup.
192-
193-
To enable the status and ping pages uncomment the line in the
194-
`stats.example.com.conf` virtual host configuration file.
195-
196-
## Valid referrers and resource usage constraining
197-
198-
Note that this configuration assumes that you are stating exactly
199-
**which** hosts can use your Piwik installation. In the example
200-
config, for all static files, i.e., images, Javascript, Flash and
201-
CSS there is a `valid_referers` block where all allowed hosts are
202-
enumerated. You should replace the `*.mysite.com` and
203-
`othersite.com` with the hosts where you want Piwik to be used for
204-
analytics.
205-
206-
If that is too much of an hassle for you, then just comment out the
207-
`valid_referers` block.
208-
209-
If you are using this configuration and you are not getting any
210-
results for a particular site where you have Piwik enabled, then
211-
first check for the `valid_referers` block. To see if that host is
212-
enumerated there.
213-
214-
## Blacklisting User Agents and Referers
215-
216-
There are some serious issues with some User Agents out there. Some
217-
are operated in a bandwidth hogging fashion. Implementing bots that
218-
use and abuse the site is bandwidth. Even more serious is when the User
219-
Agent is used for exploits. Trying to penetrate/crack the site
220-
through crafted scripts running under the cloak of a _well meaning_
221-
bot.
222-
223-
The same applies to Referers where shady sites sent traffic to you
224-
that only hijacks the bandwith and curtail the correct usage of
225-
Piwik.
226-
227-
There is a blacklist of User Agents and Referers that is disabled by
228-
default. You have to **enable it** explicitly.
229-
230-
Uncomment the `include blacklist.conf` line in the `nginx.conf`
231-
configuration file to enable User Agent and Referer blacklisting. Of
232-
course you can define your own list of blacklisted User Agents.
233-
234-
## Getting the latest Nginx packaged for Debian or Ubuntu
235-
236-
There's a [Debian repository](http://debian.perusio.net/unstable
237-
"my Debian repo") with the
238-
[latest](http://nginx.org/en/download.html "Nginx source download")
239-
version of Nginx. This is packaged for Debian **unstable** or
240-
**testing**. The instructions for using the repository are
241-
presented on this [page](http://debian.perusio.net/debian.html
242-
"Repository instructions").
243-
244-
It may work on Ubuntu. Since Ubuntu seems to appreciate more
245-
finding semi-witty names for their releases instead of making clear
246-
what is the status of the software included. Is it
247-
**stable**? Is it **testing**? Is it **unstable**? The package may
248-
work with your currently installed environment or not. I don't have
249-
the faintest idea which release to advise. So you are on your
250-
own. Generally the APT machinery will sort out for you any
251-
dependencies issues that might exist.
252-
253-
## Other Nginx configs on github
254-
255-
+ [Drupal](https://github.com/perusio/drupal-with-nginx "Drupal
256-
Nginx configuration")
257-
258-
+ [WordPress](https://github.com/perusio/wordpress-nginx "WordPress Nginx
259-
configuration")
260-
261-
+ [Chive](https://github.com/perusio/chive-nginx "Chive Nginx
262-
configuration")
263-
264-
+ [Redmine](https://github.com/perusio/redmine-nginx "Redmine Nginx
265-
configuration")
266-
267-
+ [SquirrelMail](https://github.com/perusio/squirrelmail-nginx
268-
"SquirrelMail Nginx configuration")
269-
270-
## Securing your PHP configuration
271-
272-
There's a small shell script that parses your `php.ini` and
273-
sets a sane environment, be it for **development** or
274-
**production** settings.
275-
276-
Grab it [here](https://github.com/perusio/php-ini-cleanup "PHP
277-
cleanup script").
278-
279-
## Keeping PHP always running
280-
281-
php-fpm is hardly perfect, or better said, it happens quite often
282-
that PHP has extremely long running processes that can completely
283-
clog PHP processing. To obviate that I've created a shell script
284-
that relaunches php-fpm whenever PHP hangs.
285-
286-
Grab it [here](https://github.com/perusio/php-relaunch-web).
287-
288-
## Authors
289-
290-
This configuration is maintained by
291-
[celogeek](https://github.com/geistteufel) and
292-
[perusio](https://github.com/perusio).
12+
- clone this repostitory or [download it as a zip](https://github.com/Findus23/nginx/archive/master.zip) then move its content to `/etc/nginx/` (or wherever you store your nginx-config)
13+
- read through the `sites-available/matomo.conf` and modify the settings to fit your use case:
14+
- set `server_name` to the domain(s) of your Matomo instance
15+
- set the path to your SSL certificate (I really recommend you to make sure your Matomo instance is only reachable via HTTPS. If you don't have an SSL certificate for your domain yet, check out [Let's Encrypt](https://letsencrypt.org/).)
16+
- do you want to support old browsers? Then you'll need to modify `ssl.conf` accoring to your need. (the [Mozilla SSL Config Generator](https://mozilla.github.io/server-side-tls/ssl-config-generator/) will help you)
17+
- replace `/var/www/matomo/` with the path to your Matomo instance
18+
- configure PHP (this depends on your OS and PHP setup)
19+
- if you are using fastcgi (which is probably the case) set `fastcgi_pass` to the path of your PHP socket file
20+
- you can also specify a TCP port
21+
- go to the `sites-enabled` folder of your nginx config directory
22+
- enable the Matomo config by creating a symlink: `sudo ln -s ../sites-available/matomo.conf`
23+
- test if there is a syntax error in your config: `sudo nginx -t`
24+
- restart nginx: `sudo systemctl restart`

apps/piwik/fcgi_piwik_cache.conf

Lines changed: 0 additions & 30 deletions
This file was deleted.

apps/piwik/fcgi_piwik_long_cache.conf

Lines changed: 0 additions & 30 deletions
This file was deleted.

0 commit comments

Comments
 (0)