运行3个网站在一个域名上使用nginx

huangapple go评论77阅读模式
英文:

Run 3 websites on one domain using nginx

问题

我有一个域名abc.com,它重定向到我的服务器IP 1.2.3.4。我在服务器上运行了3个网站,使用Docker容器:

  1. 第一个网站的IP是1.2.3.4:50/a
  2. 第二个网站的IP是1.2.3.4:60/b
  3. 第三个网站的IP是1.2.3.4:70/c

我可以通过IP访问这些网站,但我想使用我的域名来运行这三个网站,例如:

  • abc.com/a 应该运行第一个网站
  • abc.com/b 应该运行第二个网站
  • abc.com/c 应该运行第三个网站

我正在使用RHEL上的Nginx服务器版本1.14.1,请帮助我。我尝试了以下配置文件:

  1. location /a {
  2. rewrite ^/a(.*)$ http://1.2.3.4:50/a redirect;
  3. }
  4. location /b {
  5. rewrite ^/b(.*)$ http://1.2.3.4:60/b redirect;
  6. }

但它会重定向并在URL中显示端口。

英文:

Lets say i have domain abc.com which redirects my server ip 1.2.3.4

i have 3 websites running on my server using docker container,

  1. 1st website ip 1.2.3.4:50/a
  2. 2nd website ip 1.2.3.4:60/b
  3. 3rd website ip 1.2.3.4:70/c

i can access this all websites using ip

i want to use my domain to run these three websites.
e.g.

  1. abc.com/a should run 1st website
  2. abc.com/b should run 2nd website
  3. abc.com/c should run 3rd website

i am using nginx server version 1.14.1 on RHEL

Please help

i tried these configuration file

  1. location /a {
  2. rewrite ^/a(.*)$ http://1.2.3.4:50/a redirect;
  3. }
  4. location /b {
  5. rewrite ^/b(.*)$ http://1.2.3.4:60/b redirect;
  6. }

but it is redirecting and displaying the port in the url

答案1

得分: 0

尝试使用以下方式解决您的问题:

  1. location /a {
  2. proxy_pass http://1.2.3.4:50;
  3. proxy_set_header Host $host;
  4. proxy_set_header X-Real-IP $remote_addr;
  5. proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  6. }
  7. location /b {
  8. proxy_pass http://1.2.3.4:60;
  9. proxy_set_header Host $host;
  10. proxy_set_header X-Real-IP $remote_addr;
  11. proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  12. }
  13. location /c {
  14. proxy_pass http://1.2.3.4:70;
  15. proxy_set_header Host $host;
  16. proxy_set_header X-Real-IP $remote_addr;
  17. proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  18. }

关于此代理配置的信息,它将使您的 abc.com/a 访问 http://1.2.3.4:50 网站的根目录。但是 /a 也将被传递到 http://1.2.3.4:50,因此您不必为每个代理添加 http://1.2.3.4:50/a。

您可以在 nginx.conf 中显式配置它:

  1. // Nginx.conf
  2. http {
  3. ...
  4. server {
  5. server_name abc.com;
  6. // 这里是 location ...
  7. }
  8. }

或者通过在 sites-available 中包含配置,通常在 nginx.conf 中可以找到此部分:

  1. // Nginx.conf
  2. http {
  3. ...
  4. include /etc/nginx/sites-available/*;
  5. ...
  6. }

然后,您可以在 sites-available 文件夹中添加 site.confmain.conf,例如具有以下配置:

  1. server {
  2. server_name abc.com;
  3. // 这里是 location ...
  4. }
英文:

Try this to solve your problem

  1. location /a {
  2. proxy_pass http://1.2.3.4:50;
  3. proxy_set_header Host $host;
  4. proxy_set_header X-Real-IP $remote_addr;
  5. proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  6. }
  7. location /b {
  8. proxy_pass http://1.2.3.4:60;
  9. proxy_set_header Host $host;
  10. proxy_set_header X-Real-IP $remote_addr;
  11. proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  12. }
  13. location /c {
  14. proxy_pass http://1.2.3.4:70;
  15. proxy_set_header Host $host;
  16. proxy_set_header X-Real-IP $remote_addr;
  17. proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  18. }

For your information, this proxy configuration will make your abc.com/a to access the root of your http://1.2.3.4:50 website. But the /a will also be passed to the http://1.2.3.4:50 so you don't have to bother add http://1.2.3.4:50/a for each proxy

You can configure it explicitly inside the nginx.conf

  1. // Nginx.conf
  2. http {
  3. ...
  4. server {
  5. server_name abc.com;
  6. // here is location ...
  7. }
  8. }

Or by including configurations in sites-available, usually in the nginx.conf you can found this section:

  1. // Nginx.conf
  2. http {
  3. ...
  4. include /etc/nginx/sites-available/*;
  5. ...
  6. }

Then you can add site.conf or main.conf, you name it yourself for example in sites-available folder that has this kind of configuration:

  1. server {
  2. server_name abc.com;
  3. // here is location ...
  4. }

huangapple
  • 本文由 发表于 2023年2月14日 19:09:27
  • 转载请务必保留本文链接:https://go.coder-hub.com/75446961.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定