Skip to content

Instantly share code, notes, and snippets.

@dannymichel
Last active November 11, 2016 15:03
Show Gist options
  • Save dannymichel/50fb2a02a19a038daed78d4d27d940d8 to your computer and use it in GitHub Desktop.
Save dannymichel/50fb2a02a19a038daed78d4d27d940d8 to your computer and use it in GitHub Desktop.
# You may add here your
# server {
# ...
# }
# statements for each of your virtual hosts to this file
##
# You should look at the following URL's in order to grasp a solid understanding
# of Nginx configuration files in order to fully unleash the power of Nginx.
# http://wiki.nginx.org/Pitfalls
# http://wiki.nginx.org/QuickStart
# http://wiki.nginx.org/Configuration
#
# Generally, you will want to move this file somewhere, and start with a clean
# file but keep this around for reference. Or just disable in sites-enabled.
#
# Please see /usr/share/doc/nginx-doc/examples/ for more detailed examples.
##
server {
server_name s.example.com;
rewrite ^ https://$server_name$request_uri? permanent;
}
server {
listen 443 ssl;
server_name s.example.com;
ssl_certificate /etc/ssl/certs/s.example.com.crt;
ssl_certificate_key /etc/ssl/private/s.example.com.key;
ssl_protocols TLSv1.1 TLSv1.2;
ssl_ciphers 'ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-AES256-GCM-SHA384:DHE-RSA-AES128-GCM-SHA256:DHE-DSS-AES128-GCM-SHA256:kEDH+AESGCM:ECDHE-RSA-AES128-SHA256:ECDHE-ECDSA-AES128-SHA256:ECDHE-RSA-AES128-SHA:ECDHE-ECDSA-AES128-SHA:ECDHE-RSA-AES256-SHA384:ECDHE-ECDSA-AES256-SHA384:ECDHE-RSA-AES256-SHA:ECDHE-ECDSA-AES256-SHA:DHE-RSA-AES128-SHA256:DHE-RSA-AES128-SHA:DHE-DSS-AES128-SHA256:DHE-RSA-AES256-SHA256:DHE-DSS-AES256-SHA:DHE-RSA-AES256-SHA:!aNULL:!eNULL:!EXPORT:!DES:!RC4:!3DES:!MD5:!PSK';
ssl_prefer_server_ciphers on;
root /usr/share;
index index.php index.html index.htm;
# Make site accessible from http://localhost/
location / {
# First attempt to serve request as file, then
# as directory, then fall back to displaying a 404.
try_files $uri $uri/ =404;
# Uncomment to enable naxsi on this location
# include /etc/nginx/naxsi.rules
}
error_page 404 /404.html;
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
location ~ \.php$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php7-fpm.sock;
fastcgi_index index.php;
include fastcgi_params;
}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}
# another virtual host using mix of IP-, name-, and port-based configuration
#
#server {
# listen 8000;
# listen somename:8080;
# server_name somename alias another.alias;
# root html;
# index index.html index.htm;
#
# location / {
# try_files $uri $uri/ =404;
# }
#}
# HTTPS server
#
#server {
# listen 443;
# server_name localhost;
#
# root html;
# index index.html index.htm;
#
# ssl on;
# ssl_certificate cert.pem;
# ssl_certificate_key cert.key;
#
# ssl_session_timeout 5m;
#
# ssl_protocols SSLv3 TLSv1 TLSv1.1 TLSv1.2;
# ssl_ciphers "HIGH:!aNULL:!MD5 or HIGH:!aNULL:!MD5:!3DES";
# ssl_prefer_server_ciphers on;
#
# location / {
# try_files $uri $uri/ =404;
# }
#}
2016/11/11 09:52:10 [error] 11880#11880: *40 directory index of "/var/www/" is forbidden, client: 108.162.219.124, server: s.example.com, request: "GET / HTTP/1.1", host: "example.com"
2016/11/11 09:52:12 [error] 11880#11880: *41 directory index of "/var/www/" is forbidden, client: 108.162.219.124, server: s.example.com, request: "GET / HTTP/1.1", host: "example.com"
2016/11/11 09:52:56 [error] 11880#11880: *46 directory index of "/var/www/" is forbidden, client: 108.162.219.124, server: s.example.com, request: "GET / HTTP/1.1", host: "example.com"
2016/11/11 09:57:19 [crit] 11881#11881: *77 connect() to unix:/var/run/php7-fpm.sock failed (2: No such file or directory) while connecting to upstream, client: 108.162.219.124, server: s.example.com, request: "GET / HTTP/1.1", upstream: "fastcgi://unix:/var/run/php7-fpm.sock:", host: "example.com"
2016/11/11 09:57:19 [error] 11881#11881: *77 open() "/var/www/nginx/html/50x.html" failed (2: No such file or directory), client: 108.162.219.124, server: s.example.com, request: "GET / HTTP/1.1", upstream: "fastcgi://unix:/var/run/php7-fpm.sock", host: "example.com"
2016/11/11 09:57:22 [crit] 11881#11881: *76 connect() to unix:/var/run/php7-fpm.sock failed (2: No such file or directory) while connecting to upstream, client: 108.162.219.124, server: s.example.com, request: "GET / HTTP/1.1", upstream: "fastcgi://unix:/var/run/php7-fpm.sock:", host: "example.com"
2016/11/11 09:57:22 [error] 11881#11881: *76 open() "/var/www/nginx/html/50x.html" failed (2: No such file or directory), client: 108.162.219.124, server: s.example.com, request: "GET / HTTP/1.1", upstream: "fastcgi://unix:/var/run/php7-fpm.sock", host: "example.com"
2016/11/11 09:59:53 [error] 11988#11988: *1 directory index of "/var/www/" is forbidden, client: 108.162.219.124, server: s.example.com, request: "GET / HTTP/1.1", host: "example.com"
2016/11/11 10:00:51 [error] 11988#11988: *8 directory index of "/var/www/" is forbidden, client: 108.162.219.124, server: s.example.com, request: "GET / HTTP/1.1", host: "example.com"
2016/11/11 10:01:31 [crit] 11988#11988: *12 connect() to unix:/var/run/php7-fpm.sock failed (2: No such file or directory) while connecting to upstream, client: 108.162.219.124, server: s.example.com, request: "GET / HTTP/1.1", upstream: "fastcgi://unix:/var/run/php7-fpm.sock:", host: "example.com"
2016/11/11 10:01:31 [error] 11988#11988: *12 open() "/var/www/nginx/html/50x.html" failed (2: No such file or directory), client: 108.162.219.124, server: s.example.com, request: "GET / HTTP/1.1", upstream: "fastcgi://unix:/var/run/php7-fpm.sock", host: "example.com"
2016/11/11 10:01:34 [crit] 11988#11988: *12 connect() to unix:/var/run/php7-fpm.sock failed (2: No such file or directory) while connecting to upstream, client: 108.162.219.124, server: s.example.com, request: "GET / HTTP/1.1", upstream: "fastcgi://unix:/var/run/php7-fpm.sock:", host: "example.com"
2016/11/11 10:01:34 [error] 11988#11988: *12 open() "/var/www/nginx/html/50x.html" failed (2: No such file or directory), client: 108.162.219.124, server: s.example.com, request: "GET / HTTP/1.1", upstream: "fastcgi://unix:/var/run/php7-fpm.sock", host: "example.com"
2016/11/11 10:02:02 [crit] 11988#11988: *21 connect() to unix:/var/run/php7-fpm.sock failed (2: No such file or directory) while connecting to upstream, client: 108.162.219.124, server: s.example.com, request: "GET / HTTP/1.1", upstream: "fastcgi://unix:/var/run/php7-fpm.sock:", host: "example.com"
2016/11/11 10:02:02 [error] 11988#11988: *21 open() "/var/www/nginx/html/50x.html" failed (2: No such file or directory), client: 108.162.219.124, server: s.example.com, request: "GET / HTTP/1.1", upstream: "fastcgi://unix:/var/run/php7-fpm.sock", host: "example.com"
## example.com
server {
server_name example.com www.example.com www.exampleazine.com exampleazine.com autoconfig.* autodiscover.*;
rewrite ^ https://example.com$request_uri? permanent;
#rewrite ^/autodiscover/autodiscover\.xml$ /autodiscover/autodiscover.php;
}
server {
listen 443 ssl;
listen [::]:443 ssl default_server ipv6only=on;
include /etc/nginx/ssl.conf;
server_name example.com;
root /home/example/public_html;
set $no_cache 0;
# Don't cache the CMS admin area
location /admin {
set $no_cache 1;
}
## maintenance mode ##
######################
# set $maintenance on;
# if ($remote_addr ~ (8.8.8.8|8.8.4.4)) {
# set $maintenance off;
# }
# if ($uri ~ ^/(index.php/)?(paypal)/(.*)$ ) {
# set $maintenance off;
# }
# if ($maintenance = on) {
# return 503;
# }
# location /maintenance {}
# error_page 503 @maintenance;
# location @maintenance {
# root /var/public_html/example.com/maintenance;
# rewrite ^(.*)$ /index.html break;
# }
##########################
## end maintenance mode ##
location = /favicon.ico {
try_files $uri =204;
}
location ~* \.(jpe?g|gif|css|png|js|ico|pdf|zip|tar|t?gz|mp3|wav|swf)$ {
expires max;
}
location / {
index index.html index.php;
try_files $uri $uri/ @handler;
#rewrite ^/my-url.html /my-url/ permanent;
#rewrite ^/my-url.html http://domain.co.uk/ permanent;
expires 7d;
}
location /shop {
# rewrite ^/shop /shop/index.php; NO
try_files $uri /shop/index.php;
}
location /shop/downloader {
# rewrite ^/shop/downloader /shop/downloader/index.php; NO
try_files $uri /shop/downloader/index.php;
}
#location /blog {
#rewrite ^/blog.html /blog/ permanent;
#index index.php index.html index.htm;
#try_files $uri $uri/ /blog/index.php;
#}
location ~ ^/(app|includes|media/downloadable|pkginfo|report/config.xml|var)/ { deny all; }
location ~ ^/(info.php|var/export/)/ {
auth_basic "Restricted Access";
auth_basic_user_file /etc/nginx/htpasswd;
}
location @handler {
rewrite / /index.php;
}
location ~ .php/ {
rewrite ^(.*.php)/ $1 last;
}
location ~ .php$ {
fastcgi_pass unix:/var/run/php7-fpm.sock;
fastcgi_index index.php;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param SCRIPT_NAME $fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info;
fastcgi_param MAGE_RUN_TYPE website;
fastcgi_param MAGE_RUN_CODE base;
fastcgi_cache phpcache; # The name of the cache key-zone to use
fastcgi_cache_valid 200 30m; # What to cache: 'code 200' responses, for half an hour
fastcgi_cache_methods GET HEAD; # What to cache: only GET and HEAD requests (ot POST)
add_header X-Fastcgi-Cache $upstream_cache_status; # Allow us to see if the cache was HIT, MISS, or BYPASSED inside a browser's Inspector panel
fastcgi_cache_bypass $no_cache; # Dont pull from the cache if true
fastcgi_no_cache $no_cache; # Dont save to the cache if true
include fastcgi_params;
}
location ~* ^/autodiscover/autodiscover.xml$ {
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME /home/example/public_html/autodiscover/autodiscover.php;
#fastcgi_param PHP_VALUE "include_path=.:/usr/share/php:/usr/share/pear:/usr/share/awl/inc";
fastcgi_pass unix:/var/run/php7-fpm.sock;
}
#Yoast sitemap
location ~ ([^/]*)sitemap(.*)\.x(m|s)l$ {
## this redirects sitemap.xml to /sitemap_index.xml
rewrite ^/sitemap\.xml$ /sitemap_index.xml permanent;
## this makes the XML sitemaps work
rewrite ^/([a-z]+)?-?sitemap\.xsl$ /index.php?xsl=$1 last;
rewrite ^/sitemap_index\.xml$ /index.php?sitemap=1 last;
rewrite ^/([^/]+?)-sitemap([0-9]+)?\.xml$ /index.php?sitemap=$1&sitemap_n=$2 last;
## The following lines are optional for the premium extensions
## News SEO
rewrite ^/news-sitemap\.xml$ /index.php?sitemap=wpseo_news last;
## Local SEO
rewrite ^/locations\.kml$ /index.php?sitemap=wpseo_local_kml last;
rewrite ^/geo-sitemap\.xml$ /index.php?sitemap=wpseo_local last;
## Video SEO
rewrite ^/video-sitemap\.xsl$ /index.php?xsl=video last;
}
include /etc/nginx/minify.conf;
include /etc/nginx/scripts.conf;
location /pomf {
try_files $uri $uri/ =404;
auth_basic "Restricted Content";
auth_basic_user_file /home/example/public_html/pomf/.htpasswd;
}
# BEGIN W3TC Minify cache
location ~ /wp-content/cache/minify.*\.js$ {
types {}
default_type application/x-javascript;
expires modified 31536000s;
add_header Vary "Accept-Encoding";
add_header Pragma "public";
add_header Cache-Control "max-age=31536000, public";
}
location ~ /wp-content/cache/minify.*\.css$ {
types {}
default_type text/css;
expires modified 31536000s;
add_header Vary "Accept-Encoding";
add_header Pragma "public";
add_header Cache-Control "max-age=31536000, public";
}
location ~ /wp-content/cache/minify.*js\.gzip$ {
gzip off;
types {}
default_type application/x-javascript;
expires modified 31536000s;
add_header Vary "Accept-Encoding";
add_header Pragma "public";
add_header Cache-Control "max-age=31536000, public";
add_header Content-Encoding gzip;
}
location ~ /wp-content/cache/minify.*css\.gzip$ {
gzip off;
types {}
default_type text/css;
expires modified 31536000s;
add_header Vary "Accept-Encoding";
add_header Pragma "public";
add_header Cache-Control "max-age=31536000, public";
add_header Content-Encoding gzip;
}
# END W3TC Minify cache
# BEGIN W3TC Browser Cache
#gzip on;
#gzip_types text/css text/x-component application/x-javascript application/javascript text/javascript text/x-js text/richtext image/svg+xml text/plain text/xsd text/xsl text/xml image/x-icon;
location ~ \.(css|htc|less|js|js2|js3|js4)$ {
expires 31536000s;
add_header Pragma "public";
add_header Cache-Control "max-age=31536000, public";
}
location ~ \.(htm|rtf|rtx|svg|svgz|xsd|xsl|xml)$ {
expires 3600s;
add_header Pragma "public";
add_header Cache-Control "max-age=3600, public";
}
location ~ \.(asf|asx|wax|wmv|wmx|avi|bmp|class|divx|doc|docx|eot|exe|gif|gz|gzip|ico|jpg|jpeg|jpe|json|mdb|mid|midi|mov|qt|mp3|m4a|mp4|m4v|mpeg|mpg|mpe|mpp|otf|odb|odc|odf|odg|odp|ods|odt|ogg|pdf|png|pot|pps|ppt|pptx|ra|ram|svg|svgz|swf|tar|tif|tiff|ttf|ttc|wav|wma|wri|woff|xla|xls|xlsx|xlt|xlw|zip)$ {
expires 31536000s;
add_header Pragma "public";
add_header Cache-Control "max-age=31536000, public";
}
# END W3TC Browser Cache
# BEGIN W3TC Minify core
rewrite ^/wp-content/cache/minify.*/w3tc_rewrite_test$ /wp-content/plugins/w3-total-cache/pub/minify.php?w3tc_rewrite_test=1 last;
set $w3tc_enc "";
if ($http_accept_encoding ~ gzip) {
set $w3tc_enc .gzip;
}
if (-f $request_filename$w3tc_enc) {
rewrite (.*) $1$w3tc_enc break;
}
rewrite ^/wp-content/cache/minify/(.+/[X]+\.css)$ /wp-content/plugins/w3-total-cache/pub/minify.php?test_file=$1 last;
rewrite ^/wp-content/cache/minify/(.+\.(css|js))$ /wp-content/plugins/w3-total-cache/pub/minify.php?file=$1 last;
# END W3TC Minify core
}
user www-data;
worker_processes 4;
pid /run/nginx.pid;
events {
worker_connections 768;
# multi_accept on;
}
http {
include /etc/nginx/mime.types;
default_type application/octet-stream;
fastcgi_cache_path /etc/nginx-cache levels=1:2 keys_zone=phpcache:100m inactive=60m;
fastcgi_cache_key "$scheme$request_method$host$request_uri";
log_format main '$remote_addr - $remote_user [$time_local] "$request "'
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /var/log/nginx/access.log main;
sendfile off;
autoindex off;
map $scheme $fastcgi_https { ## Detect when HTTPS is used
default off;
https on;
}
keepalive_timeout 10;
gzip on;
gzip_comp_level 2;
gzip_proxied any;
gzip_types text/plain text/css application/x-javascript text/xml application/xml application/xml+rss text/javascript;
# Load config files from the /etc/nginx/conf.d directory
#include /etc/nginx/conf.d/*.conf;
include /etc/nginx/sites-enabled/*;
client_max_body_size 60M;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment