gliders.nginx.conf 3.18 KB
Newer Older
Owain Jones's avatar
Owain Jones committed
1 2 3
# https://www.nginx.com/resources/wiki/start/topics/recipes/symfony/
# NOTE: This makes any .php file other than app, app_dev and config.php
# downloadable. If you want one to run, add it to the "pass the PHP scripts to
root's avatar
root committed
4
# upstream FCGI" location section below. Be careful adding files with
Owain Jones's avatar
Owain Jones committed
5 6
# sensitive data like passwords inside them.

7 8
limit_req_zone $binary_remote_addr zone=gliders:10m rate=10r/s;

9 10
server {
    listen 80;
root's avatar
root committed
11

12
    server_name ~(?:((?<symfonyenv>.+)\.)?gliders.*|mars\.noc\.ac\.uk);
Owain Jones's avatar
Owain Jones committed
13

14 15 16 17
    if ($symfonyenv) {
        set $symfonyfile "app_$symfonyenv";
        set $symfonyroot "/var/gliders/www_$symfonyenv";
    }
18

19 20 21 22 23
    if ($symfonyenv = "") {
        set $symfonyfile "app";
        set $symfonyroot "/var/gliders/www";
        set $symfonyenv "prod";
    }
root's avatar
root committed
24

25
    root $symfonyroot/web;
root's avatar
root committed
26

Owain Jones's avatar
Owain Jones committed
27
    error_log /var/log/nginx/gliders.error.log;
28
    access_log /var/log/nginx/$symfonyenv.gliders.access.log;
root's avatar
root committed
29

root's avatar
root committed
30 31 32 33 34
    # Enable compression. Very helpful when we're getting stuff like
    # large amounts of vehicle tracks in JSON form!
    gzip on;
    gzip_types text/plain application/xml text/html text/css application/json application/x-javascript text/xml text/javascript;

Owain Jones's avatar
Owain Jones committed
35
    # strip app.php/ prefix if it is present
36
    rewrite ^/$symfonyfile\.php/?(.*)$ /$1 permanent;
Owain Jones's avatar
Owain Jones committed
37

root's avatar
root committed
38
    location / {
39
    	limit_req zone=gliders burst=5;
40
        set $index_file $symfonyfile".php";
41 42 43 44
     	if (-f $document_root/.maintenance) {
            return 503;
        }
        index $index_file;
root's avatar
root committed
45 46 47
        try_files $uri @rewriteapp;
    }

48
    error_page 413 /413.html;
Owain Jones's avatar
Owain Jones committed
49
    error_page 502 /502.html;
50
    error_page 504 /504.html;
51 52 53 54 55
    error_page 503 @maintenance;
    location @maintenance {
        rewrite ^(.*)$ /maintenance.html break;
    }

root's avatar
root committed
56 57 58 59 60 61 62 63
    location ~ ^/(images|fonts|css|js) {
        access_log off;
        error_log off;
        try_files $uri =404;
        expires 1y;
        add_header Cache-Control "public";
    }

root's avatar
root committed
64 65 66 67 68 69
    location ~ ^/images/vehicles/(?P<vehicle_type>[a-zA-Z0-9]+)_(?P<vehicle_name>[a-zA-Z0-9]+)\.(png|jpg)$ {
        access_log off;
        error_log off;
        try_files $uri images/vehicles/$vehicle_name.png =404;
    }

root's avatar
root committed
70 71 72 73
    location /static/logs {
        alias /var/gliders/logs;
    }

74 75 76 77
    location /var/gliders/logs {
        alias /var/gliders/logs;
    }

78 79
    location /static {
        alias /var/gliders/static;
root's avatar
root committed
80 81
    }

root's avatar
root committed
82
    location @rewriteapp {
83
        rewrite ^(.*)$ /${symfonyfile}.php/$1 last;
root's avatar
root committed
84 85 86 87
    }

    # pass the PHP scripts to FastCGI server from upstream phpfcgi
    location ~ ^/(app|app_dev|config)\.php(/|$) {
88 89
        # fastcgi_pass hhvmfcgi;
        fastcgi_pass phpfcgi;
90
        fastcgi_intercept_errors on;
91 92
        # error_page 502 = @fallback;
        error_page 502 /502.html;
root's avatar
root committed
93 94
        include fastcgi_params;
        fastcgi_split_path_info ^(.+\.php)(/.*)$;
95
        fastcgi_param  SCRIPT_FILENAME $symfonyroot/web$fastcgi_script_name;
root's avatar
root committed
96 97
        fastcgi_param  HTTPS off;
    }
98

99 100 101 102 103 104 105 106 107
    # location @fallback {
    #     fastcgi_pass phpfcgi;
    #    fastcgi_intercept_errors on;
    #    include fastcgi_params;
    #    fastcgi_split_path_info ^(.+\.php)(/.*)$;
    #    fastcgi_param SCRIPT_FILENAME $symfonyroot/web$fastcgi_script_name;
    #    fastcgi_param HTTPS off;
    #    error_page 502 /502.html;
    #}
root's avatar
root committed
108
}