Nginx 中的简单 URI 重定向/别名

Nginx 中的简单 URI 重定向/别名

请问,如何在 Nginx 中完成这个简单的操作:

我希望每个 uri 请求都类似: http://example.com/adm 或者: http://example.com/adm 转到 wordpress 登录页面:wordpress/wp-login.php(wordpress 安装在“wordpress”文件夹内)。

我试过了:

location /adm/ { alias wordpress/wp-login.php; }
location /adm { alias wordpress/wp-login.php; }

和:

rewrite ^/adm$ wordpress/wp-login.php;

但没有成功...更糟糕的是,有些规则,如“rewrite /adm wordpress/wp-login.php;”(确实有效!),一旦用户登录,就会自动将一些请求重定向到默认的 wordpress 管理页面...我不知道为什么...

以防有人问,这是我的服务配置文件:

server {
    server_name www.example.com;
    rewrite ^ http://example.com$request_uri? permanent;
}

server {
    server_name example.com;

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

    root /var/www/example.com;

    index index.htm index.php;

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

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

    location = /favicon.ico {log_not_found off;access_log off;}
    location = /robots.txt {allow all;log_not_found off;access_log off;}
    location ~ /\. {deny all;access_log off;log_not_found off;  }
}

谢谢。

答案1

我需要同样的东西,但上面的答案对我来说有点奇怪。对于其他正在寻找解决方案的人来说。我最终使用了以下内容:

location ~* /login/ {
    rewrite ^/login/(.*)? /wp-admin/$1;
}

答案2

Nginx 论坛的朋友们帮我找到了一个解决方案:http://forum.nginx.org/read.php?2,202235,202475#msg-202475

他们得出的结论是:

location = /adm/ {
    fastcgi_pass 127.0.0.1:9000;
    fastcgi_param SCRIPT_FILENAME /var/www/example.com/wordpress/wp-login.php
    include /etc/nginx/fastcgi_params;
}

location = /adm {
    fastcgi_pass 127.0.0.1:9000;
    fastcgi_param SCRIPT_FILENAME /var/www/example.com/wordpress/wp-login.php
    include /etc/nginx/fastcgi_params;
}

相关内容