简体   繁体   中英

Starting http-server on Amazon EC2

I'm trying to setup and access a simple Python http-server on an Amazon EC2 instance. The server works locally under localhost:80, but I cannot get it to work on the EC2 instance.

I connected an elastic IP to the instance, and it at least works to access ssh on that IP.

I think the problem is that I'm setting the server_address for the HTTPServer incorrectly: Python HTTPServer documentation Since I really don't have a clue, at home I configured the router to forward HTTP requests to the computer's local IP, 192.168.1.xx

I thought that it might be a security group issue, so I added inbound HTTP with source 0.0.0.0/0 (which I thought should allow all incoming IPs) and sets port 80 without any option to change.

Additionally, I think that running the server script under sudo on port 80 is a potential security risk. Is there any way to forward the request under another port like 8080 instead of 80?

For the security group, you can create a rule that allows port 8080 (Do "Custome TCP Rule", then put 8080 for the port range).

It sounds likely that your HTTPServer is binding to loopback. I would set the address to 0.0.0.0 (all interfaces), set the port to 8080, and open 8080 on your security group.

From a security point of view it might be better to run your script behind a regular http server.

Here is a configuration for NginX that should get you started (puth this file in /etc/nginx/conf.d/ ):

upstream myscript  {
  server localhost:8080;
}

server {
    server_name _;
    listen 80;
    location = / {
        proxy_pass  http://myscript;
    }
}

In this setting, you run your script on port 8080 (use server_address = ('localhost', 8080) to configure your HTTPServer in your python script).

So when a query hits nginx on port 80 it forwards it to localhost port 8080 where your script takes in charge.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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