ruiruige1991
ruiruige1991

Reputation: 675

Nginx reverse proxy with 301 redirect under https

I am a new in Nginx, my aim is that:

when I visit 127.0.0.1:8080/proxy/git/ or Https://127.0.0.1/proxy/git/, the Nginx (reverse) proxy can make it to https://github.com

My nginx conf is blow:

http {

    server {   
    listen  8080 default backlog=2048;  
    listen  443 ssl;

    server_name  127.0.0.1;  

    ssl_certificate /etc/nginx/xxxxxxx.crt;
    ssl_certificate_key /etc/nginx/xxxxxxx.key;

    location /proxy/git/ {   
        proxy_pass https://github.com/;
        proxy_set_header   X-Forwarded-For $proxy_add_x_forwarded_for; 
        proxy_pass_header Server;
        proxy_redirect off;

        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Scheme $scheme;

        }
        error_page   500 502 503 504  /50x.html;   
   }

    ##
    # Basic Settings
    ##

    sendfile on; 
    tcp_nopush on; 
    tcp_nodelay on; 
    keepalive_timeout 65; 
    types_hash_max_size 2048;
    # server_tokens off;

    # server_names_hash_bucket_size 64; 
    # server_name_in_redirect off;

when I enter or in my chrome address bar, I got a 301 redirect and the address became , which I think it became a direct visit, but I want a proxy visit, here is a picture:

enter image description here

However, when I redirect to a http&static website, it's OK.

I have searched a lot from the website, somebody says it's because proxy_redirect, I have tested it to proxy_redirect / /; or delete it, and change the proxy_set_header Host $host; into proxy_set_header Host $http_host; but all make no sense.

specially, if I both delete the proxy_redirectline and change $host to $http_hosts, like below:

        proxy_pass https://github.com/;
        proxy_set_header   X-Forwarded-For $proxy_add_x_forwarded_for; 
        proxy_pass_header Server;
        #proxy_redirect default;

        proxy_set_header Host $http_host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Scheme $scheme;

it seems that it will do 301 beteewn myself and myself ???

enter image description here

So, my question is:

  1. Can anybody tell me how can I reverse proxy to github under 127.0.0.1? (Imagine I am in a place where must use this kind reverse proxy)

  2. It's very nice of you if you can use simple words to explain why we should do that, I also want to know the reason.

  3. My english is very poor , so in order to make it clear, can we call github the "target server" or "target website", and call 127.0.0.1 "proxy server" or "nginx"

Thanks sincerely for anyone who wants to help me!

Upvotes: 2

Views: 8294

Answers (1)

ruiruige1991
ruiruige1991

Reputation: 675

I have found the reason.

Github needs Https and HOST:github.com

the proxy_set_header Host $http_host;can not help you change this host header, should use proxy_set_header Host $proxy_host; instead. This will change the host header to be github.

Upvotes: 5

Related Questions