0

I am completely new to NGINX, never even touched it, however I'm in a little situation where looking online points to needing to use it.

My situation is this:

I have 1 server (Windows Server) and I want to run two FTP servers and two webservers. Now, I know I cannot host two applications on the same port (say port 80 for both webservers etc) and therefore I will have to put them on different ports.

For this situation, lets say I have two subdomains one.example.com and two.example.com. Both of these subdomains point to my server's IP address 1.2.3.4. In any normal situation, accessing either webserver and either FTP server through either subdomain will be possible, on the different ports I have assigned. This is not what I want.

What i'd like to do is have one FTP server and one webserver on one subdomain, and on port 21 and 80, and the other FTP server and webserver on the other subdomain, and again on port 21 and 80.

As far as I can tell, I need to make it that you can only access certain things (filter) on certain subdomains, and the ports of those things (21 and 80) get translated to other ports on the server (as you can't have two applications residing on one port).

If I can, i'd like to run NGINX on the windows server, but if not I really would need it running on something small like my Raspberry Pi, even if that requires my Pi to be a gateway between the internet and my network.

EDIT: It is important for me to note here that I intend to run more than just FTP and HTTP on this server, so this port translation and domain filtering needs to work on any kind of socket application, not just FTP, HTTP etc. If NGINX is not the solution, any other working solution is ok.

6
  • While you might be able to use a reverse proxy in front of your server for http to direct specific URLs to specific IP addresses and ports, you will NOT be able to do anything about FTP. FTP does not have host headers like HTTP does. Feb 14, 2018 at 6:27
  • @Appleoddity The intention here is to get a "dumb" port change. Any information that comes on the subdomain for port 21 would get statically sent to another port on the server, regardless of what is being sent and received. Feb 14, 2018 at 6:30
  • Are you talking reverse proxies or just virtualhosts?
    – Journeyman Geek
    Feb 14, 2018 at 6:43
  • Nothing virtualised here, so I assume proxies @JourneymanGeek Feb 14, 2018 at 6:48
  • no no. A virtualhost's basically "If the host is A, do X, if the host is B, do Y"
    – Journeyman Geek
    Feb 14, 2018 at 6:52

1 Answer 1

0

ngnix is a lot easier on linux - at least documentationwise. I'm having a feeling we're going to have to start at the start though. ngnix typically works out of the box, though you'd need to set up things like php seperately - there's no mod proxy equivalent.

In any modern HTTP server there's some flavour of virtualhosts. in ngnix you can either have them set up as a single file or seperate files. This kind of won't work on FTP - but there's workarounds. (Ngnix calls it server blocks but same difference).

Just have seperate accounts in ftp with different usernames, passwords and rood directories.

Virtualhosts only works on http and https and basically is a way of directing traffic on different domains, to different routes, letting you run multiple services on different root directories based on the hostname. For example you can run two instances of wordpress on the same server with mostly separate files.

I'm lazy - I have one configuration file per domain. If you're running this on windows - do look up the appropriate documentation for details.

You'll want to locate where the per site config files are - mine's in /etc/ngnix/sites-available, and these are symlinked to sites-enabled. this may based on the platform

You will always have a default setup there that's sane and should work. Its boring and you can read through it to understand what the various options are.

You can either edit that (I don't recommend it) or create a per site config file in the same folder. This is an example of the one I use. This overrides the defaults

server {
        listen 80;
        listen [::]:80;


        root /var/www/html/new;

        # Add index.php to the list if you are using PHP
        index index.html index.htm index.nginx-debian.html index.php;

        server_name new.example.com;

        location / {
                # First attempt to serve request as file, then
                # as directory, then fall back to displaying a 404.
                try_files $uri $uri/ =404;
        }

        # pass the PHP scripts to FastCGI server via unix socks
        #
        location ~ \.php$ {
                include snippets/fastcgi-php.conf;

                fastcgi_pass unix:/run/php/php7.0-fpm.sock;
        }

        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        location ~ /\.ht {
                deny all;
        }
}

So, more or less, I tell it to listen to port 80, on ipv4 and 6, tell it the hostname to listen to, and some other php related misc stuff.

If I change root and swap new.example.com with new2.example.com and change the root, it'll send any requests to new2.example.com to the content in the new root. You can, for example, run 2 copies of wordpress on the same server with different domains that way. I create one of these with the appropriate server_name and root per domain in sites-enabled, symlink them to sites enabled (might work differently in windows) and reload my nginix config.

Assuming you're trying to proxy You'd have one or more servers on different ports. ngnix sits in front to direct traffic

server {
        listen 80;
        listen [::]:80;
        server_name matrix.example.com;



        location /_matrix {
        proxy_pass http://localhost:8008;
        proxy_set_header X-Forwarded-For $remote_addr;
 }

}

Consider this config file - I'm not sure it will actually work (I run a HTTPS enabled version of this), but that's what docs are for. It'll listen in on port 80, for the domain matrix.example.com.

7
  • The OP asked how to run multiple instances on different ports. Are you suggesting this as an alternative method? Feb 14, 2018 at 13:34
  • Yup! And tried to explain what each thing does.
    – Journeyman Geek
    Feb 14, 2018 at 14:26
  • @JourneymanGeek This is exactly what I want from the webserver perspective, but my intention of putting FTP in there was to be a stand-in for any other application. For example, I may also intend to run multiple game servers, telnet/SSH servers etc, and therefore I want to make it that only certain server applications can be accessed on certain subdomains, and specific ports upon those subdomains. I'm not just looking at HTTP based applications. Feb 15, 2018 at 8:46
  • That gets tricky fast. The server needs to be aware of hostnames for that to work. serverfault.com/questions/255055/… is what I'd refer to but srv records might not work in all cases. HTTP is built for what you want, so it works. Other protocols may or may not.
    – Journeyman Geek
    Feb 15, 2018 at 8:50
  • @journeymanGeek SRV records appear to be something that the client connecting has to listen to. What I am really looking for is some kind of firewall type filtering system, but a bit like NAT, translating a requested port on a requested subdomain to an application on one machine, and blocking requests if the port being accessed is not allowed to be accessed on a subdomain. Feb 15, 2018 at 9:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.