| 1 | fastcgi_cache_path /path/to/cache levels=1:2 keys_zone=redirects_prod:10m;
|
|---|
| 2 |
|
|---|
| 3 | server {
|
|---|
| 4 | listen 80;
|
|---|
| 5 | server_name hostname;
|
|---|
| 6 | server_name_in_redirect off;
|
|---|
| 7 |
|
|---|
| 8 | set $web_root /web/root/;
|
|---|
| 9 | root $web_root;
|
|---|
| 10 |
|
|---|
| 11 | error_log /var/log/nginx/prod_error.log warn;
|
|---|
| 12 | access_log /var/log/nginx/prod_access.log main_ext;
|
|---|
| 13 |
|
|---|
| 14 | set $prerender_fallback_file index.html;
|
|---|
| 15 | error_page 404 /404.html;
|
|---|
| 16 |
|
|---|
| 17 | add_header X-Cache $upstream_cache_status;
|
|---|
| 18 |
|
|---|
| 19 | location /api {
|
|---|
| 20 | try_files $uri /app.php$is_args$args;
|
|---|
| 21 | }
|
|---|
| 22 |
|
|---|
| 23 | location / {
|
|---|
| 24 | try_files $uri $uri/ @redirect;
|
|---|
| 25 | }
|
|---|
| 26 |
|
|---|
| 27 | location = / {
|
|---|
| 28 | try_files asdfg @redirect;
|
|---|
| 29 | }
|
|---|
| 30 |
|
|---|
| 31 | # PROD
|
|---|
| 32 | location ~ ^/app\.php(/|$) {
|
|---|
| 33 | fastcgi_pass unix:/var/run/php5-fpm.sock;
|
|---|
| 34 | fastcgi_split_path_info ^(.+\.php)(/.*)$;
|
|---|
| 35 | include fastcgi_params;
|
|---|
| 36 |
|
|---|
| 37 | fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
|
|---|
| 38 | fastcgi_param DOCUMENT_ROOT $realpath_root;
|
|---|
| 39 |
|
|---|
| 40 | #include fastcgi_cache;
|
|---|
| 41 | #fastcgi_cache fcgi_cache;
|
|---|
| 42 |
|
|---|
| 43 | internal;
|
|---|
| 44 | }
|
|---|
| 45 |
|
|---|
| 46 | location @redirect {
|
|---|
| 47 | fastcgi_pass unix:/var/run/php5-fpm.sock;
|
|---|
| 48 | fastcgi_split_path_info ^(.+\.php)(/.*)$;
|
|---|
| 49 | include fastcgi_params;
|
|---|
| 50 |
|
|---|
| 51 | set $redirect_script /redirect.php;
|
|---|
| 52 | fastcgi_param SCRIPT_FILENAME $realpath_root$redirect_script;
|
|---|
| 53 | fastcgi_param DOCUMENT_ROOT $realpath_root;
|
|---|
| 54 |
|
|---|
| 55 | fastcgi_cache_key "$request_uri_without_query";
|
|---|
| 56 | fastcgi_cache_valid 301 302 418 24h;
|
|---|
| 57 | fastcgi_cache_bypass $arg_test_spa $arg_preview_code;
|
|---|
| 58 | fastcgi_no_cache $arg_test_spa $arg_preview_code;
|
|---|
| 59 | fastcgi_cache redirects_prod;
|
|---|
| 60 |
|
|---|
| 61 | error_page 418 =200 @prerender;
|
|---|
| 62 | error_page 502 =200 @prerender;
|
|---|
| 63 | error_page 404 /404.html;
|
|---|
| 64 | }
|
|---|
| 65 |
|
|---|
| 66 | location @prerender {
|
|---|
| 67 | set $prerender 0;
|
|---|
| 68 | if ($http_user_agent ~* "yandex|baiduspider|twitterbot|facebookexternalhit|rogerbot|linkedinbot|embedly|quora link preview|showyoubot|outbrain|pinterest|slackbot|vkShare|W3C_Validator") {
|
|---|
| 69 | set $prerender 1;
|
|---|
| 70 | }
|
|---|
| 71 | if ($args ~ "_escaped_fragment_") {
|
|---|
| 72 | set $prerender 1;
|
|---|
| 73 | }
|
|---|
| 74 | if ($http_user_agent ~ "Prerender") {
|
|---|
| 75 | set $prerender 0;
|
|---|
| 76 | }
|
|---|
| 77 | if ($uri ~ "\.(js|css|xml|less|png|jpg|jpeg|gif|pdf|doc|txt|ico|rss|zip|mp3|rar|exe|wmv|doc|avi|ppt|mpg|mpeg|tif|wav|mov|psd|ai|xls|mp4|m4a|swf|dat|dmg|iso|flv|m4v|torrent|ttf|woff)") {
|
|---|
| 78 | set $prerender 0;
|
|---|
| 79 | }
|
|---|
| 80 |
|
|---|
| 81 | if ($prerender = 1) {
|
|---|
| 82 | rewrite .* /$scheme://$host$request_uri? break;
|
|---|
| 83 | proxy_pass http://$prerender_host;
|
|---|
| 84 | }
|
|---|
| 85 | if ($prerender = 0) {
|
|---|
| 86 | rewrite .* /$prerender_fallback_file break;
|
|---|
| 87 | }
|
|---|
| 88 | }
|
|---|
| 89 | }
|
|---|