首页 > 解决方案 > WordPress Nginx 安全最佳实践

问题描述

我正在考虑从 Apache 切换到 Nginx,我正在努力确定 WordPress 特定的安全指令需要什么。到目前为止,我发现的几乎所有特定于 WordPress 的东西都会破坏 wp-admin、破坏内容布局、提供 ajax 错误或更多。

两个最常见的建议似乎是: ethanpil/wp-secure.confdigital ocean

有什么建议或更好的来源吗?

谢谢

安德鲁

标签: wordpressnginxnginx-config

解决方案


我已经把安全的 WordPress NGINX 配置的基本部分放在一起,在这里

最安全的方法是将可以通过 PHP 解释器运行的文件列入白名单,并以 HTTP 状态 404 拒绝所有其他文件的请求。

    location / {

        # any URI without extension is routed through PHP-FPM (WordPress controller)
        location ~ ^[^.]*$ {
            length_hiding on;
            fastcgi_param SCRIPT_FILENAME $document_root/index.php;
            include includes/php-example.com.conf;
        }

        # allow only a handful of PHP files in root directory to be interpreted
        # wp-cron.php ommited on purpose as it should *not* be web accessible, see proper setup
        # https://www.getpagespeed.com/web-apps/wordpress/wordpress-cron-optimization
        location ~ ^/wp-(?:links-opml|login|mail|signup|trackback)\.php$ {
            length_hiding on;
            fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
            include includes/php-example.com.conf;
        }

        # other PHP files "do not exist"
        location ~ \.php$ {
            return 404;
        }
    }

最后,禁用以下任何 PHP 执行至关重要/wp-content/

    location /wp-content/ { 
        # contents under wp-content are typically highly cacheable
        immutable on;
        # hide and do not interpret internal plugin or user uploaded scripts
        location ~ \.php$ {
            return 404;
        }
    }

如果您觉得必须将某些脚本作为/wp-content/some.phpURI 启动,那么您可能有一个需要删除的坏插件,或者(很少)如果要通过 PHP-FPM 运行则将其列入白名单。


推荐阅读