How to setup two block server (Ip based) with two differents ports with Nginx on CentOS?

89 views Asked by At

I'm on CentOS 8 my nginx configuration file is in /etc/nginx/nginx.conf

I don't have any DNS therefore I would just use the server ip to access the server, I search a way for host two website in one Nginx server (without DNS)

Thank you for your help :))

Ps. I think the best way is using two differents ports, but I don't know how

1

There are 1 answers

0
Ivan Shatsky On BEST ANSWER

I think your options are

  • Use two different ports, for example:

    server {
        listen 80 default_server; # listening port for site #1
        ... # site #1 configuration
    }
    server {
        listen 81 default_server; # listening port for site #2
        ... # site #2 configuration
    }
    

    If you have SELinux enabled system, ports used for this configuration must be labeled as http_port_t type, otherwise nginx won't be able to bind on those ports. You can check avaliable http_port_t ports with

    semanage port -l|grep http_port_t
    

    command. To label some new port with http_port_t type (for example, port 81), use

    semanage port -a -t http_port_t -p tcp 81
    

    Do not forget to open used ports for external access via your firewall.

  • Serve the second site under some URI prefix, for example, use http://<ip>/ for the first site and http://<ip>/blog/ for the second one:

    server {
        listen 80 default_server;
        location / {
            # configuration for site #1
            root /path/to/site1;
            ...
        }
        location /blog/ {
            # configuration for site #2
            alias /path/to/site2/;
            ...
        }
    }
    

    Using this configuration could be tricky because every link on your site #2 must be either relative or start with /blog/ prefix, otherwise that link won't be served with location /blog/ { ... } block therefore leading to site #1.