在linux和apache上的同一个EC2中运行多个dotnet api的项目

时间:2018-01-28 07:47:16

标签: .net linux apache amazon-web-services amazon-ec2

我有两个在Linux EC2 AWS实例上发布的dotnet项目,第一个项目在端口5000上,另一个项目在50003上。 我在conf.d文件夹中创建了第一个文件:

<VirtualHost *:80>
ProxyPreserveHost On
ProxyPass / http://127.0.0.1:5000/
ProxyPassReverse / http://127.0.0.1:5000/
ErrorLog /var/log/httpd/hellomvc-error.log
CustomLog /var/log/httpd/hellomvc-access.log common

和另一个:

<VirtualHost *:80>
ProxyPreserveHost On
ProxyPass / http://127.0.0.1:5003/
ProxyPassReverse / http://127.0.0.1:5003/
ErrorLog /var/log/httpd/hellomvc-error.log
CustomLog /var/log/httpd/hellomvc-access.log common

第二个只是一个API。

我的问题是我可以在浏览器上打开第一个项目而没有任何问题但我无法访问第二个项目的API,即使我可以使用curl http://localhost:5003/api/Home

在本地访问它

1 个答案:

答案 0 :(得分:0)

除非第一个项目和第二个项目有两个不同的域名,否则您只能尝试使用一个VirtualHost:

<VirtualHost *:80>
ProxyPreserveHost On

# send /api paths to project2
ProxyPass /api http://127.0.0.1:5003/api
ProxyPassReverse /api http://127.0.0.1:5003/api

# and everything else to project1
ProxyPass / http://127.0.0.1:5000/
ProxyPassReverse / http://127.0.0.1:5000/

ErrorLog /var/log/httpd/hellomvc-error.log
CustomLog /var/log/httpd/hellomvc-access.log common
</VirtualHost>

如果您有两个使用sam路径(/api)的后端服务,则必须使用不同的主机名和多个虚拟主机:

<VirtualHost *:80>
  ServerName project1.example.com

  ProxyPreserveHost On
  ProxyPass / http://127.0.0.1:5000/
  ProxyPassReverse /api http://127.0.0.1:5000/

  ErrorLog /var/log/httpd/project1-error.log
  CustomLog /var/log/httpd/project1-access.log common
</VirtualHost>

<VirtualHost *:80>
  ServerName api1.example.com

  ProxyPreserveHost On
  ProxyPass / http://127.0.0.1:5003/
  ProxyPassReverse /api http://127.0.0.1:5003/

  ErrorLog /var/log/httpd/api1-error.log
  CustomLog /var/log/httpd/api1-access.log common
</VirtualHost>

<VirtualHost *:80>
  ServerName api2.example.com

  ProxyPreserveHost On  
  ProxyPass / http://127.0.0.1:5004/
  ProxyPassReverse /api http://127.0.0.1:5004/

  ErrorLog /var/log/httpd/api2-error.log
  CustomLog /var/log/httpd/api2-access.log common
</VirtualHost>

当然,project1.example.com,api1.example.com和api2.example.com需要解析到服务器的IP地址