質問

I inherited a site running on nginx. A number pages (dozens) need to go away and be redirected, but the bulk (hundreds) of pages do not need to be redirected. What is the right syntax to say 'allow all pages except those in this list to be returned normally'? The closest I could get is the following code inside the http directive:

# nginx configuration
server {    
    listen someurl.org:80;
    location /about/strategic-plan {
   return 301 http://www.anothersite.com;
}   
}    

The snippet correctly redirects the page /about/strategic-plan to http://www.anothersite.com, but all other page requests generate 404s. This also true if I use rewrite directive rather than return directive.

If I remove the listen directive, all pages work normally, but no redirect happens for /about/strategic-plan.

My first problem: how do I get nginx to listen to a domain without having to specify a location for every page in the site?

Second: how do I get individual pages to redirect without tanking the whole site?

I must be missing something fundamental here. Any help appreciated.

役に立ちましたか?

解決

I see no root directive in your server. Also add default location.

server {       
    listen someurl.org:80;

    root /path/to/htmls;

    location / {
        # serve static files
    }

    location /about/strategic-plan {
        return 301 http://www.anothersite.com;
    }      
}
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top