WordPress Nginx安全最佳实践

问题描述

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

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

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

谢谢

安德鲁

解决方法

我已经将安全的WordPress NGINX配置的基本组成部分放在一起,here

最安全的方法是将可以通过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;
        }
    }

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

    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.php URI的身份启动某些脚本,则可能有一个需要删除的错误插件,或者如果要通过PHP-FPM运行,则有(很少)白名单。