大网数据广告活动
服务器运维问答

Appending an argument to request_uri for mobile subdomain on nginx

· 52CE 我爱测

I'm running a multilingual wiki (MediaWiki 1.26.2 with MobileFrontend ) on nginx 1.9.3/OpenBSD 5.8. For each language wiki, I have a separate MediaWiki installation folder and a subdomain like en.domain.com pointing to that folde…

问题原文

原帖链接 · Till Kraemer · CC BY-SA 3.0

I'm running a multilingual wiki (MediaWiki 1.26.2 with MobileFrontend) on nginx 1.9.3/OpenBSD 5.8.

For each language wiki, I have a separate MediaWiki installation folder and a subdomain like en.domain.com pointing to that folder.

I'd like to add a subdomain like en.m.domain.com for the mobile view using the MediaWiki installation folder of the desktop view but with an appended &mobileaction=toggle_view_mobile (or ?mobileaction=toggle_view_mobile with a question mark instead of an ampersand if there is already an argument).

I'm also using CORS, short URLs and redirects from http:// to https://.

This is how my server block looks like:

server {
    listen 80;
    server_name en.m.domain.com;
    root /path/to/domain/en;
    index index.html index.htm index.php;
    autoindex off;

# CORS
    add_header 'Access-Control-Allow-Origin' '*';
    add_header 'Access-Control-Allow-Credentials' 'true';
    add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
    add_header 'Access-Control-Allow-Headers' 'X-Requested-With, Accept, Content-Type, Origin';

# Redirect to https://

    if ($http_cf_visitor ~ '{"scheme":"http"}') {
        return 301 https://$server_name$request_uri;
        }

    location = / {
        return 301 https://en.m.domain.com/wiki/Main_Page;
        }

    location = /w {
        return 301 https://en.m.domain.com/wiki/Main_Page;
        }

    location = /w/ {
        return 301 https://en.m.domain.com/wiki/Main_Page;
        }

    location = /wiki {
        return 301 https://en.m.domain.com/wiki/Main_Page;
        }

    location = /wiki/ {
        return 301 https://en.m.domain.com/wiki/Main_Page;
        }

# Short URLs

    location / {
        index index.php;
        error_page 404 = @mediawiki;
        }

    location @mediawiki {
        rewrite ^/wiki([^?]*)(?:\?(.*))? /w/index.php?title=$1&$2 last;
        }

    location ~ \.php5?$ {
        try_files $uri =404;
        include fastcgi_params;
        fastcgi_pass   127.0.0.1:1234;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_intercept_errors on;
        }

    location ~ \.php?$ {
        try_files $uri =404;
        include fastcgi_params;
        fastcgi_pass   127.0.0.1:1234;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_intercept_errors on;
        }

# Append mobileaction=toggle_view_mobile for mobile version

    location / {                                                   

        # If there are no arguments add a question mark
        if ($args = '') {                                            
        set $new_request_uri "$request_uri?mobileaction=toggle_view_mobile";  
        }                                                            

        # If there are already arguments add an ampersand
        if ($args != "") {      
        set $new_request_uri "$request_uri&mobileaction=toggle_view_mobile";  
        }

        rewrite $new_request_uri last;        
        }       

}

Unfortunately the mobileaction=toggle_view_mobile part doesn't work :(

Any ideas how to fix this?

Thanks and cheers,

Till

已采纳解答

原帖链接 · Richard Smith · CC BY-SA 3.0

There are multiple problems with your current implementation: you have two location / blocks and rewrite $new_request_uri last; is semantically incorrect.

The easy solution is to modify the $request_uri by performing an external redirect. This is messy because you need to identify only those URIs which do not have a mobileaction argument. For example:

if ($args !~* mobileaction) {
    rewrite ^ $uri?mobileaction=toggle_view_mobile permanent;
}

The rewrite directive takes care of ? vs & and automatically appends the existing argument list.

The if block could be placed inside the location / block or above it.

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2016-01-31)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 3.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。