|
| 1 | +# Nginx VHOST for NPP-Optimized WordPress Setup # |
| 2 | +# ---------------------------------------------------------------------------- # |
| 3 | +# Author: [Hasan ÇALIŞIR] # |
| 4 | +# Purpose: Full-stack Dockerized environment for NPP WordPress plugin, including # |
| 5 | +# WordPress, PHP-FPM, Nginx, MySQL, WP-CLI, and necessary PHP extensions. # |
| 6 | +# ---------------------------------------------------------------------------- # |
| 7 | + |
| 8 | +# FastCGI Cache Path |
| 9 | +#################### |
| 10 | +fastcgi_cache_path /var/cache/nginx levels=1:2 keys_zone=NPP:100m max_size=400m inactive=30d; |
| 11 | +#################### |
| 12 | + |
| 13 | +server { |
| 14 | + # Listen HTTP |
| 15 | + ################################## |
| 16 | + listen 80; |
| 17 | + server_name localhost; |
| 18 | + |
| 19 | + # HTTPS Redirect |
| 20 | + ################################## |
| 21 | + location / { |
| 22 | + rewrite ^ https://$host$request_uri? permanent; |
| 23 | + } |
| 24 | +} |
| 25 | + |
| 26 | +server { |
| 27 | + # Listen HTTPS |
| 28 | + ################################## |
| 29 | + listen 443 ssl; |
| 30 | + server_name localhost; |
| 31 | + |
| 32 | + # Document root |
| 33 | + ############### |
| 34 | + root /var/www/html; |
| 35 | + index index.php index.html index.htm; |
| 36 | + |
| 37 | + # Cache Settings |
| 38 | + ################# |
| 39 | + set $skip_cache 0; |
| 40 | + |
| 41 | + if ($query_string != "") { |
| 42 | + set $skip_cache 1; |
| 43 | + } |
| 44 | + |
| 45 | + if ($request_uri ~* "(?:/add-to-cart=.*|/cart/.*|/my-account/.*|/checkout/.*|/wp-json/.*|/wc-api/.*|/addons.*|/wp-admin(?:/.*)?|/robots\.txt|/xmlrpc\.php|wp-.*\.php|/feed/.*|index\.php|sitemap(_index)?\.xml|[a-z0-9_-]+-sitemap([0-9]+)?\.(xml|html)|/sitemap\.html)") { |
| 46 | + set $skip_cache 1; |
| 47 | + } |
| 48 | + |
| 49 | + if ($http_cookie ~* "comment_author|wordpress_[a-f0-9]+|wp-postpass|wordpress_no_cache|wordpress_logged_in") { |
| 50 | + set $skip_cache 1; |
| 51 | + } |
| 52 | + |
| 53 | + if ($http_cookie ~* "woocommerce_items_in_cart") { |
| 54 | + set $skip_cache 1; |
| 55 | + } |
| 56 | + |
| 57 | + if ($arg_add-to-cart != "") { |
| 58 | + set $skip_cache 1; |
| 59 | + } |
| 60 | + # End |
| 61 | + ##### |
| 62 | + |
| 63 | + # Stop Nginx from logging |
| 64 | + ######################### |
| 65 | + location = /favicon.ico { |
| 66 | + log_not_found off; |
| 67 | + access_log off; |
| 68 | + } |
| 69 | + |
| 70 | + location = /robots.txt { |
| 71 | + log_not_found off; |
| 72 | + access_log off; |
| 73 | + allow all; |
| 74 | + } |
| 75 | + # End |
| 76 | + ##### |
| 77 | + |
| 78 | + # Serve content |
| 79 | + ################## |
| 80 | + location / { |
| 81 | + try_files $uri $uri/ /index.php$is_args$args; |
| 82 | + } |
| 83 | + # End |
| 84 | + ##### |
| 85 | + |
| 86 | + # PHP-FPM Configuration |
| 87 | + ################ |
| 88 | + location ~ \.php$ { |
| 89 | + # Use NPP "Cache Key Regex" option for non-default structers |
| 90 | + # This structer supported in default by NPP |
| 91 | + ############################################################ |
| 92 | + fastcgi_cache_key "$scheme$request_method$host$request_uri"; |
| 93 | + ############################################################ |
| 94 | + |
| 95 | + try_files $uri =404; |
| 96 | + fastcgi_split_path_info ^(.+\.php)(/.+)$; |
| 97 | + fastcgi_index index.php; |
| 98 | + fastcgi_pass wordpress-fpm:9001; |
| 99 | + include /etc/nginx/fastcgi_params; |
| 100 | + fastcgi_cache_bypass $skip_cache; |
| 101 | + fastcgi_no_cache $skip_cache; |
| 102 | + fastcgi_cache NPP; |
| 103 | + fastcgi_cache_valid 30d; |
| 104 | + fastcgi_cache_use_stale error timeout updating invalid_header http_500 http_503; |
| 105 | + fastcgi_cache_lock on; |
| 106 | + } |
| 107 | + # End |
| 108 | + ##### |
| 109 | + |
| 110 | + # WP Security Settings |
| 111 | + ####################### |
| 112 | + # Deny access to hidden files and directories (except .well-known) |
| 113 | + location ~ /\.(?!well-known/) { |
| 114 | + deny all; |
| 115 | + } |
| 116 | + |
| 117 | + # Deny access to wp-config files and other critical files |
| 118 | + location ~* ^/(?:wp-config\.php|wp-config-sample\.php|readme\.html|license\.txt)$ { |
| 119 | + deny all; |
| 120 | + } |
| 121 | + |
| 122 | + # Deny access to PHP files in uploads, files, plugins, themes |
| 123 | + location ~* /(?:uploads|files|wp-content|wp-includes)/.*\.php$ { |
| 124 | + deny all; |
| 125 | + } |
| 126 | + |
| 127 | + # Deny access to script file types |
| 128 | + location ~* .(pl|cgi|py|sh|lua|asp)$ { |
| 129 | + deny all; |
| 130 | + } |
| 131 | + |
| 132 | + # Block access to wp-content plugin/theme log files |
| 133 | + location ~* ^/wp-content/(plugins|themes)/.*\.(txt|log|md)$ { |
| 134 | + deny all; |
| 135 | + } |
| 136 | + |
| 137 | + # Block common attack patterns |
| 138 | + location ~* "(eval\(.*\)|base64_encode\(.*\)|\{0\}|phpinfo|self/environ)" { |
| 139 | + deny all; |
| 140 | + } |
| 141 | + |
| 142 | + # Block dangerous file access |
| 143 | + location ~* "/(thumb|timthumb|config|settings|sqlpatch|webshell|phpinfo)\.php" { |
| 144 | + deny all; |
| 145 | + } |
| 146 | + # End |
| 147 | + ##### |
| 148 | + |
| 149 | + # Browser Cache (Enable on Production) |
| 150 | + ############### |
| 151 | + # location ~*.(ogg|ogv|svg|svgz|eot|otf|woff|mp4|ttf|css|rss|atom|js|jpg|jpeg|gif|png|ico|zip|tgz|gz|rar|bz2|doc|xls|exe|ppt|tar|mid|midi|wav|bmp|rtf)$ { |
| 152 | + # expires max; |
| 153 | + # log_not_found off; |
| 154 | + # access_log off; |
| 155 | + # } |
| 156 | + # End |
| 157 | + ##### |
| 158 | + |
| 159 | + # Dummy Self-Signed SSL Certificate (Don't use on Production) |
| 160 | + ################# |
| 161 | + ssl_certificate /etc/ssl/fullchain.pem; |
| 162 | + ssl_certificate_key /etc/ssl/privkey.pem; |
| 163 | + ssl_trusted_certificate /etc/ssl/chain.pem; |
| 164 | + # End |
| 165 | + ##### |
| 166 | + |
| 167 | + # SSL Settings (Enable on Production) |
| 168 | + ############## |
| 169 | + # ssl_protocols TLSv1.2 TLSv1.3; |
| 170 | + # ssl_prefer_server_ciphers on; |
| 171 | + # ssl_ciphers ECDHE-ECDSA-CHACHA20-POLY1305:ECDHE-RSA-CHACHA20-POLY1305:ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384:DHE-RSA-AES128-GCM-SHA256:DHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-AES128-SHA256:ECDHE-RSA-AES128-SHA256:ECDHE-ECDSA-AES128-SHA:ECDHE-RSA-AES256-SHA384:ECDHE-RSA-AES128-SHA:ECDHE-ECDSA-AES256-SHA384:ECDHE-ECDSA-AES256-SHA:ECDHE-RSA-AES256-SHA:DHE-RSA-AES128-SHA256:DHE-RSA-AES128-SHA:DHE-RSA-AES256-SHA256:DHE-RSA-AES256-SHA:ECDHE-ECDSA-DES-CBC3-SHA:ECDHE-RSA-DES-CBC3-SHA:EDH-RSA-DES-CBC3-SHA:AES128-GCM-SHA256:AES256-GCM-SHA384:AES128-SHA256:AES256-SHA256:AES128-SHA:AES256-SHA:DES-CBC3-SHA:!DSS; |
| 172 | + # ssl_ecdh_curve secp384r1; |
| 173 | + # ssl_session_cache shared:SSLA:10m; |
| 174 | + # ssl_session_timeout 10m; |
| 175 | + # ssl_session_tickets off; |
| 176 | + # ssl_buffer_size 4k; |
| 177 | + # End |
| 178 | + ##### |
| 179 | + |
| 180 | + # Security Headers (Enable on Production) |
| 181 | + ################## |
| 182 | + # add_header Content-Security-Policy upgrade-insecure-requests; |
| 183 | + # add_header Permissions-Policy "microphone=(), camera=(), interest-cohort=(), browsing-topics=()"; |
| 184 | + # add_header Referrer-Policy no-referrer-when-downgrade; |
| 185 | + # add_header X-Frame-Options "SAMEORIGIN"; |
| 186 | + # add_header X-Content-Type-Options "nosniff"; |
| 187 | + # add_header X-XSS-Protection "1; mode=block"; |
| 188 | + # add_header Strict-Transport-Security "max-age=31536000; includeSubDomains; preload" always; |
| 189 | + # End |
| 190 | + ##### |
| 191 | + |
| 192 | + # FastCGI Cache Status Header |
| 193 | + ############################# |
| 194 | + add_header NPP-FastCGI-Cache $upstream_cache_status; |
| 195 | + ############################# |
| 196 | +} |
0 commit comments