Score:1

nginx: How to redirect to another domain while using password-authentication?

pl flag

I'm trying to redirect domain.com to domain2.com using Nginx. And the user should only be redirected with a correct password.

I achieved the redirecting and the password-authentication. But combing those two doesn't work.

Here is my /etc/nginx/nginx.conf:

server {
            listen 80;
            server_name domain.com;

            location /thisOne {
                auth_basic "Restricted Content";
                auth_basic_user_file /etc/nginx/.htpasswd;
                return 301 http://domain2.com:8080/thisOne/;
            }
        }

Can someone please help me? What am I doing wrong?

Score:0
us flag

The problem is that the return statement is evaluated before the auth statements. You can move the return statement into another block to force the evaluation order you are looking for.

For example:

location /thisOne {
    auth_basic "Restricted Content";
    auth_basic_user_file /etc/nginx/.htpasswd;
    try_files _nonexistent_ @thisOne;
}
location @thisOne {
    return 301 http://domain2.com:8080/thisOne/;
}
pl flag
Thanks! That solved my problem!
pl flag
And how do I combine that with proxy_pass? If I write "proxy_pass" instead of "return 301", it says syntax error.
Richard Smith avatar
us flag
`proxy_pass` does not suffer from the same early evaluation problem as `return`, so it should work with your original configuration.
pl flag
Oh okay. So I tried it with ` location /thisOne { auth_basic "Restricted Content"; auth_basic_user_file /etc/nginx/.htpasswd; proxy_pass http://domain2.com:8080/thisOne/; } ` but than I get an Redirect-Error saying: the called website redirects the request so that it can never be completed
pl flag
I got it. I used the IP for domain2 instead of domain2 and it worked.
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.