37

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.

4

7 回答 7

44

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;
}
于 2013-07-19T05:26:04.013 回答
33

I prefer to use:

location = / {
    return 301 http://$host/wiki/Main_Page;
}
于 2016-12-27T17:03:45.990 回答
3
  1. Make sure "/wiki/Main_Page" can be successfully accessed
  2. Check in the server section, there are no global rewrite rules. Rewrite rules in server section will be executed before location section.
  3. Using rewrite rules in location section like this:

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

Pay attention "break" here. Means break out the rewrite cycle.

If this page is located in backend server, here should use proxy_pass.

于 2013-07-19T05:30:22.397 回答
2

The answer you used is a redirect, making you skip the / location to a /wiki location, You can try this instead

location = / {
    rewrite ^ /w/index.php?title=Main_Page&$args last;
}

This should server the Main_Page for the / URI

于 2013-07-19T12:12:43.813 回答
0

Add rewrite directive to server section:

server {
   ...
   rewrite ^/$ /wiki/Main_Page
   ...
}
于 2020-04-07T14:35:19.613 回答
0

If you want to subdir's HTML file for default index.html. For example, copy /main/mainView.html to /index.html and use base tag in index.html to set relative location's base path.

于 2020-12-12T09:02:28.047 回答
-1

Here's my solution:

if ($uri = '/'){
    rewrite ^/(.*)$ http://example.com/wiki/Main_Page permanent;
}
于 2019-06-10T05:35:21.573 回答