AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / server / 问题 / 753033
Accepted
Till Kraemer
Till Kraemer
Asked: 2016-02-01 09:11:22 +0800 CST2016-02-01 09:11:22 +0800 CST 2016-02-01 09:11:22 +0800 CST

在 nginx 上为移动子域的 request_uri 添加一个参数

  • 772

我在 nginx 1.9.3/OpenBSD 5.8 上运行多语言 wiki(MediaWiki 1.26.2 和MobileFrontend )。

对于每个语言 wiki,我都有一个单独的 MediaWiki 安装文件夹和一个指向该文件夹的子域(如 en.domain.com)。

我想使用桌面视图的 MediaWiki 安装文件夹为移动视图添加像 en.m.domain.com 这样的子域,但附加一个&mobileaction=toggle_view_mobile(或者?mobileaction=toggle_view_mobile如果已经有参数,则使用问号而不是 & 号) .

我还使用 CORS、短 URLhttp://和从to重定向https://。

这是我的服务器块的样子:

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;        
        }       

}

不幸的是,这mobileaction=toggle_view_mobile部分不起作用:(

任何想法如何解决这一问题?

谢谢和欢呼,

直到

nginx
  • 1 1 个回答
  • 814 Views

1 个回答

  • Voted
  1. Best Answer
    Richard Smith
    2016-02-02T04:19:08+08:002016-02-02T04:19:08+08:00

    您当前的实现存在多个问题:您有两个location /块并且rewrite $new_request_uri last;在语义上不正确。

    简单的解决方案是$request_uri通过执行外部重定向来修改。这很麻烦,因为您只需要识别那些没有mobileaction参数的 URI。例如:

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

    该rewrite指令负责?vs&并自动附加现有的参数列表。

    if块可以放置在块内部location /或上方。

    • 1

相关问题

  • Gzip 与反向代理缓存

  • nginx 作为代理的行为

  • Nginx 学习资源 [关闭]

  • 提供 70,000 个静态文件 (jpg) 的最佳方式?

  • 在 Apache、LightTPD 和 Nginx Web 服务器上提供 PHP 5.x 应用程序的现状?

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    新安装后 postgres 的默认超级用户用户名/密码是什么?

    • 5 个回答
  • Marko Smith

    SFTP 使用什么端口?

    • 6 个回答
  • Marko Smith

    命令行列出 Windows Active Directory 组中的用户?

    • 9 个回答
  • Marko Smith

    什么是 Pem 文件,它与其他 OpenSSL 生成的密钥文件格式有何不同?

    • 3 个回答
  • Marko Smith

    如何确定bash变量是否为空?

    • 15 个回答
  • Martin Hope
    Tom Feiner 如何按大小对 du -h 输出进行排序 2009-02-26 05:42:42 +0800 CST
  • Martin Hope
    Noah Goodrich 什么是 Pem 文件,它与其他 OpenSSL 生成的密钥文件格式有何不同? 2009-05-19 18:24:42 +0800 CST
  • Martin Hope
    Brent 如何确定bash变量是否为空? 2009-05-13 09:54:48 +0800 CST
  • Martin Hope
    cletus 您如何找到在 Windows 中打开文件的进程? 2009-05-01 16:47:16 +0800 CST

热门标签

linux nginx windows networking ubuntu domain-name-system amazon-web-services active-directory apache-2.4 ssh

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve