将根地址重写为Nginx中的子目录 [英] Rewrite root address to a subdirectory in nginx

查看:371
本文介绍了将根地址重写为Nginx中的子目录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将我的mediawiki网站转换为使用nginx作为静态文件的前端,并在php的后端使用apache.到目前为止,我已经一切正常,除了当我查看根目录"example.com"时,它会尝试提供目录列表并给出403错误,因为我已禁用该功能并且那里没有索引文件.

我现在制定的apache重写规则很简单:

RewriteRule ^$ /wiki/Main_Page [L]

我尝试使用nginx中的location指令进行类似的操作,但是它不起作用:

location = / {
    rewrite "^$" /wiki/Main_Page;
}

我其余的位置指令是:

location / {
    try_files $uri $uri/ @rewrite;
}

location @rewrite {
    rewrite ^/wiki/(.*)$ /w/index.php?title=$1&$args;
}

location ~* \.(js|css|png|jpg|jpeg|gif|ico)$ {
    try_files $uri /w/index.php?title=$1&$args;
    expires max;
    log_not_found off;
}

location ~ \.php?$ {
    proxy_set_header X-Real-IP  $remote_addr;
    proxy_set_header X-Forwarded-For $remote_addr;
    proxy_set_header Host $host;
    proxy_pass http://127.0.0.1:8080;
}

我可以简单地在其中放置带有header('Location:')的index.php文件,但是我宁愿使用重写规则正确地进行操作.

我在网上找到的所有有关使用nginx运行mediawiki的示例都以wiki.example.com而不是/wiki/子目录的形式运行Wiki.

我还尝试将try_files添加到这样的try_files中:try_files $uri $uri/ @rewrite /wiki/Main_Page;具有相同的403错误结果.

解决方案

我在nginx irc聊天中找到了帮助.

基本上,我需要做的是使用返回而不是重写.所以我改变了这个:

location = / {
    rewrite "^$" /wiki/Main_Page;
}

对此:

location = / {
    return 301 http://www.example.com/wiki/Main_Page;
}

I'm converting my mediawiki site to use nginx as a frontend for static files with apache on the backend for php. I've gotten everything working so far except for when I view the root directory "example.com" it tries to serve a directory listing and gives a 403 error since I have that disabled and don't have an index file there.

The apache rewrite rule I have in place right now is simply:

RewriteRule ^$ /wiki/Main_Page [L]

I tried something similar with a location directive in nginx, but it's not working:

location = / {
    rewrite "^$" /wiki/Main_Page;
}

The rest of my location directives are:

location / {
    try_files $uri $uri/ @rewrite;
}

location @rewrite {
    rewrite ^/wiki/(.*)$ /w/index.php?title=$1&$args;
}

location ~* \.(js|css|png|jpg|jpeg|gif|ico)$ {
    try_files $uri /w/index.php?title=$1&$args;
    expires max;
    log_not_found off;
}

location ~ \.php?$ {
    proxy_set_header X-Real-IP  $remote_addr;
    proxy_set_header X-Forwarded-For $remote_addr;
    proxy_set_header Host $host;
    proxy_pass http://127.0.0.1:8080;
}

I can simply put an index.php file with header('Location:') in it, but I'd rather just do it properly with a rewrite rule.

All the examples I've found online for running mediawiki with nginx run the wiki as wiki.example.com instead of a /wiki/ subdirectory.

Edit: I also tried adding to the try_files like this: try_files $uri $uri/ @rewrite /wiki/Main_Page; with the same 403 error result.

解决方案

I found help in the nginx irc chat.

Basically what I needed to do was use a return instead of rewrite. So I changed this:

location = / {
    rewrite "^$" /wiki/Main_Page;
}

to this:

location = / {
    return 301 http://www.example.com/wiki/Main_Page;
}

这篇关于将根地址重写为Nginx中的子目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆