英文:
Run 3 websites on one domain using nginx
问题
我有一个域名abc.com,它重定向到我的服务器IP 1.2.3.4。我在服务器上运行了3个网站,使用Docker容器:
- 第一个网站的IP是1.2.3.4:50/a
- 第二个网站的IP是1.2.3.4:60/b
- 第三个网站的IP是1.2.3.4:70/c
我可以通过IP访问这些网站,但我想使用我的域名来运行这三个网站,例如:
- abc.com/a 应该运行第一个网站
- abc.com/b 应该运行第二个网站
- abc.com/c 应该运行第三个网站
我正在使用RHEL上的Nginx服务器版本1.14.1,请帮助我。我尝试了以下配置文件:
location /a {
rewrite ^/a(.*)$ http://1.2.3.4:50/a redirect;
}
location /b {
rewrite ^/b(.*)$ http://1.2.3.4:60/b redirect;
}
但它会重定向并在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,
1st website ip 1.2.3.4:50/a
2nd website ip 1.2.3.4:60/b
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.
abc.com/a should run 1st website
abc.com/b should run 2nd website
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
location /a {
rewrite ^/a(.*)$ http://1.2.3.4:50/a redirect;
}
location /b {
rewrite ^/b(.*)$ http://1.2.3.4:60/b redirect;
}
but it is redirecting and displaying the port in the url
答案1
得分: 0
尝试使用以下方式解决您的问题:
location /a {
proxy_pass http://1.2.3.4:50;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
location /b {
proxy_pass http://1.2.3.4:60;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
location /c {
proxy_pass http://1.2.3.4:70;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
关于此代理配置的信息,它将使您的 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 中显式配置它:
// Nginx.conf
http {
...
server {
server_name abc.com;
// 这里是 location ...
}
}
或者通过在 sites-available 中包含配置,通常在 nginx.conf 中可以找到此部分:
// Nginx.conf
http {
...
include /etc/nginx/sites-available/*;
...
}
然后,您可以在 sites-available 文件夹中添加 site.conf
或 main.conf
,例如具有以下配置:
server {
server_name abc.com;
// 这里是 location ...
}
英文:
Try this to solve your problem
location /a {
proxy_pass http://1.2.3.4:50;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
location /b {
proxy_pass http://1.2.3.4:60;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
location /c {
proxy_pass http://1.2.3.4:70;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
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
// Nginx.conf
http {
...
server {
server_name abc.com;
// here is location ...
}
}
Or by including configurations in sites-available, usually in the nginx.conf you can found this section:
// Nginx.conf
http {
...
include /etc/nginx/sites-available/*;
...
}
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:
server {
server_name abc.com;
// here is location ...
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论