Rewrite root address to a subdirectory in nginx

后端 未结 7 752
攒了一身酷
攒了一身酷 2020-12-29 19:55

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 vi

相关标签:
7条回答
  • 2020-12-29 20:02

    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;
    }
    
    0 讨论(0)
  • 2020-12-29 20:07

    Here's my solution:

    if ($uri = '/'){
        rewrite ^/(.*)$ http://example.com/wiki/Main_Page permanent;
    }
    
    0 讨论(0)
  • 2020-12-29 20:12

    I prefer to use:

    location = / {
        return 301 http://$host/wiki/Main_Page;
    }
    
    0 讨论(0)
  • 2020-12-29 20:20

    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

    0 讨论(0)
  • 2020-12-29 20:27
    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.

    0 讨论(0)
  • 2020-12-29 20:27

    Add rewrite directive to server section:

    server {
       ...
       rewrite ^/$ /wiki/Main_Page
       ...
    }
    
    0 讨论(0)
提交回复
热议问题