【问题标题】:Include a list of allow ip address into specific location将允许 IP 地址列表包含到特定位置
【发布时间】:2019-10-10 06:17:24
【问题描述】:

我想让我的 wordpress 管理目录/文件只能从我的 IP 白名单中访问。

我希望该列表位于其他 conf 文件中,因为该列表有超过 200 个 IP。

这是我的 default.conf。我使用 docker-compose。

server {
    listen 80;
    server_name 127.0.0.1;

    root /var/www/html;
    index index.php;

    access_log /var/log/nginx/access.log;
    error_log /var/log/nginx/error.log;

    location ~* /wp-login\.php|/wp-admin/((?!admin-ajax\.php).)*$ {
        include /etc/nginx/conf.d/allowip.conf;
        deny all;
    }

    location / {
        try_files $uri $uri/ /index.php?$args;
    }

    location ~ \.php$ {
        try_files $uri =404;
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_pass wordpress:9000;
        fastcgi_index index.php;
        include fastcgi_params;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_param PATH_INFO $fastcgi_path_info;
    }
}

结果...

我在白名单中,当我访问 mysite.com/wp-admin 时,我的浏览器会下载实际的 php 文件。

另外,我刚刚意识到,无论您是否“包含”allowip.conf,allowip.conf 中的设置都是有效的。

我的问题

如何将单独文件中的白名单应用到某个目录?

【问题讨论】:

    标签: wordpress nginx docker-compose


    【解决方案1】:

    试试这个

    server {
        listen 80;
        server_name 127.0.0.1;
    
        root /var/www/html;
        index index.php;
    
        access_log /var/log/nginx/access.log;
        error_log /var/log/nginx/error.log;
    
        location ~* /wp-login\.php|/wp-admin/((?!admin-ajax\.php).)*$ {
            include /etc/nginx/conf.d/allowip.conf;
            deny all;
            try_files $uri =404;
            fastcgi_split_path_info ^(.+\.php)(/.+)$;
            fastcgi_pass wordpress:9000;
            fastcgi_index index.php;
            include fastcgi_params;
            fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
            fastcgi_param PATH_INFO $fastcgi_path_info;
        }
    
        location / {
            try_files $uri $uri/ /index.php?$args;
        }
    
        location ~ \.php$ {
            try_files $uri =404;
            fastcgi_split_path_info ^(.+\.php)(/.+)$;
            fastcgi_pass wordpress:9000;
            fastcgi_index index.php;
            include fastcgi_params;
            fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
            fastcgi_param PATH_INFO $fastcgi_path_info;
        }
    }
    

    【讨论】:

    • 谢谢,艾哈迈德。根据您的代码,我更改了 location = /wp-login.php ,现在可以按需要工作。我希望这可以保护管理区域。
    猜你喜欢
    • 1970-01-01
    • 2013-02-01
    • 2011-08-21
    • 1970-01-01
    • 2020-06-19
    • 2018-12-23
    • 2017-10-01
    • 2021-12-01
    • 2018-08-04
    相关资源
    最近更新 更多