简体   繁体   English

Symfony2:对Rest API使用不同的端口

[英]Symfony2: Use different port for Rest API

I currently starting a (Symfony2) project where I have to use a different port for HTTP-Communication of the REST-API (JSON/XML) then the normal HTML-Content. 我目前正在启动一个(Symfony2)项目,在该项目中,我必须使用与正常HTML内容不同的端口来进行REST-API(JSON / XML)的HTTP通讯。

Is this possible? 这可能吗? And what is the best practice? 最佳实践是什么? Can this be solved by (Symfony-) routing? 可以通过(Symfony-)路由解决吗? Goal is to provide the REST-API just for "internal" use (traffic coming from an internal ethernet-connection) and to "block" traffic which comes from a external connection. 目标是提供REST-API,仅用于“内部”使用(来自内部以太网连接的流量),并“阻止”来自外部连接的流量。

Additional info: 附加信息:

I add a route /api/user eg for the User Rest API and /api/projects for the Project Rest API. 我为User Rest API添加了路由/ api / user,为Project Rest API添加了/ api / projects。

Can I do something like? 我可以做类似的事情吗?

<VirtualHost *:81>
    DocumentRoot /var/www/html/[Symfony-Folder]/web/api
</VirtualHost>

maybe you can try in your apache config to match your mydomain.tld/api route to a different port. 也许您可以尝试在apache配置中将mydomain.tld / api路由匹配到其他端口。

Also in your config file you can configure défault port like this : 同样在您的配置文件中,您可以像这样配置défault端口:

# router configuration
router:
    ...
    http_port:            81

But I don't know if you can define a port for different routes but maybe you can take a look on the router configuration. 但是我不知道是否可以为不同的路由定义端口,但是也许可以看看路由器的配置。

You can do something like this I think : 我想你可以做这样的事情:

<VirtualHost *:80>
ServerName mydomain.tld/api

ProxyRequests Off
<Proxy *>
        Order deny,allow
        Allow from all
</Proxy>
    ProxyPass / http://localhost:81/
    ProxyPassReverse / http://localhost:81/
</VirtualHost>

You listen on the port 80 but you redirect the traffic who match the /api path to a different port. 您侦听端口80,但将与/ api路径匹配的流量重定向到其他端口。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM