nginx reverse proxy to pass subdomain in URL to backend

With nginx, is it possible to proxy an incoming URL for my React application to "see" it different than the user for handling data variables via React Router?

For example, a user would go to https://foo.app.live.com and my app would see the incoming request as http://localhost:3000/foo.

Some more examples:

  • https://foo.app.live.com === http://localhost:3000/foo
  • https://foo.app.live.com/login === http://localhost:3000/foo/login
  • https://foo.app.live.com/event/1 === http://localhost:3000/foo/event/1

I have test with this block, the entire URI is added onto the end of the domain:

location /(.*)$ {
  proxy_pass http://localhost:3000/$1;
  proxy_http_version 1.1;
  proxy_set_header Upgrade $http_upgrade;
  proxy_set_header Connection "";
  proxy_set_header Host $host;
  proxy_cache_bypass $http_upgrade;
  proxy_set_header X-Real-IP $remote_addr;
  proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}

What am I missing?

0
задан 7 September 2018 в 03:08
2 ответа

Это можно сделать с помощью директивы rewrite с флагом разрыва в блоке местоположения. В вашем случае это будет:

rewrite (.*) /foo$1 break;

С этой конфигурацией ваш URI изменится, но только на вышестоящий сервер, и пользователь этого не заметит. Более подробную информацию можно найти в документации nginx здесь: http://nginx.org/en/docs/http/ngx_http_rewrite_module.html Спасибо

0
ответ дан 30 December 2019 в 01:01

Попробуйте использовать эту версию:

location / {
    proxy_pass http://localhost:3000/foo$uri;
    proxy_http_version 1.1;      
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection "";
    proxy_set_header Host $host;
    proxy_cache_bypass $http_upgrade;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
0
ответ дан 30 December 2019 в 01:01

Теги

Похожие вопросы