You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

nginx.adoc 1.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. :source-highlighter: pygments
  2. :source-language: nginx
  3. == Nginx
  4. === Redirecting image requests to webp versions
  5. [source]
  6. ----
  7. http {
  8. map $http_accept $webp_suffix {
  9. "~*webp" ".webp";
  10. }
  11. server {
  12. location ~ \.(png|jpe?g)$ {
  13. try_files $uri$webp_suffix $uri =404;
  14. expires 1y;
  15. }
  16. }
  17. }
  18. ----
  19. === Redirecting to a canonical domain
  20. Instead of complex rules to redirect certain requests, just add a separate `server` block:
  21. [source]
  22. ----
  23. server {
  24. server_name www.example.com;
  25. return 301 $scheme://example.com$request_uri;
  26. }
  27. ----
  28. === Logging only partial IP addresses
  29. This assumes nginx is behind a proxy that sends a trusted `X-Forwarded-For` header, but the
  30. same can easily be done with the remote IP directly.
  31. [source]
  32. ----
  33. server {
  34. map $http_x_forwarded_for $forwarded_anon {
  35. ~(?P<ip>\d+\.\d+\.\d+)\. $ip.0;
  36. ~(?P<ip>[^:]+:[^:]+): $ip::;
  37. default 0.0.0.0;
  38. }
  39. log_format main '$remote_addr - $remote_user [$time_local] "$request" '
  40. '$status $body_bytes_sent "$http_referer" '
  41. '"$http_user_agent" "$forwarded_anon"';
  42. }
  43. ----