Cannot access sinatra app through the local network

£可爱£侵袭症+ 提交于 2019-12-20 14:10:56

问题


I have rails application. If I start it with rails s (port 3000), it works perfectly both on my machine and every device on my local network via the ip address (192.168.0.3 in my case).
I have sinatra application. If I start it with ruby app.rb (port 4567), it works perfectly on my machine, but it it is not accessible from other devices on my local network.

Both application use Thin as an app server.

Is it something related to how sinatra works?


回答1:


Try ruby app.rb -o 0.0.0.0 or ruby app.rb -e production. Either should work.




回答2:


Let me add some further info to Ivan's answer. Sinatra's README on command line says:

Sinatra applications can be run directly:

ruby myapp.rb [-h] [-x] [-e ENVIRONMENT] [-p PORT] [-o HOST] [-s
HANDLER] 

Options are:

-h # help
-p # set the port (default is 4567)
-o # set the host (default is 0.0.0.0)
-e # set the environment (default is development)
-s # specify rack server/handler (default is thin)
-x # turn on the mutex lock (default is off)

Per the document the default HOST is 0.0.0.0, but I still have to specify a "-o 0.0.0.0" just like Ivan said. Otherwise the server cannot be accessed from outside the server machine. How strange!




回答3:


Just want to add to Ivan's answer and Robert's clarification.

By default, you sinatra runs in development mode, not production mode. In development mode, the default host to which sinatra will bind is 'localhost', meaning only the local machine can interact with it.

Once you specify '-e production' your sinatra webapp is running in production mode, where the default host it binds to is 0.0.0.0, which means that it can interact with all others.

Alternatively, if you want to remain in development, specify '-o 0.0.0.0'



来源:https://stackoverflow.com/questions/16118971/cannot-access-sinatra-app-through-the-local-network

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!