Server Side Swift and NGINX

Fatih Nayebi
Server Side Swift
Published in
2 min readMar 26, 2018

Why NGINX?

Server Side Swift frameworks such as Vapor, Perfect and Kitura provide an embedded HTTP Server so there is no need to run them on a Apache or NGINX server.

Sometimes you would like to do that because of some of the following possible reasons:

  • All the rest of your servers are running on a specific web server so you would like to run your Server Side Swift over there too
  • Your security team prefers a specific Web Server
  • You need a reverse proxy so you could run multiple servers easily on a virtual machine
  • You plan to deploy it to a PaaS such as Google App Engine or Amazon Elastic Beanstalk

My preferred Web Server to run Server Side Swift is NGINX because of many reasons indicated above. 😊

Let’s see how we can use it to our benefits.

How?

NGINX is a Load Balancer, Web Server and Reverse Proxy.

The easiest way to run our Server Side Swift project with NGINX is to use it as a Reverse Proxy. Simply we can run our project in Docker or as a service in Linux and use a configuration similar to the following:

server {
listen 443 http2;
server_name your server name;

# Log files
error_log /yourserver/nginx.yourserver.error.log;
access_log /yourserver/nginx.yourserver.access.log;

# ssl
ssl on;
include /etc/nginx/snippets/ssl-params.conf;
ssl_certificate /etc/nginx/ssl/yourserver.crt;
ssl_certificate_key /etc/nginx/ssl/yourserver.key;

location / {

proxy_set_header Host $host:$server_port;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;

proxy_pass http://127.0.0.1:8888;
proxy_read_timeout 90;

proxy_redirect http://127.0.0.1:8888 http://yourserver.com;

# Required for new HTTP-based CLI
proxy_http_version 1.1;
proxy_request_buffering off;
}
}

In this example we run our server on 8888 port and NGINX will route the request coming to our domain to our Swift Server.

So we do not need to deal with ssl certificates in Server Side Swift and we can easily install them with NGINX as you see above. 😃

Is n’t it great?!

I typically run my Server Side Swift project inside a Docker container as HTTP and use NGINX to configure my SSL certificate. This way I can deploy my server anywhere I like easily. I use Google Cloud Platform (GCP) App Engine to run some of my servers and I do not need to get and configure certificates and it already has the NGINX configured. I just need to run my Server Side Swift on 8888! 😊

Benefits

Using NGINX as a reverse proxy with Server Side Swift simplifies the deployment. It also works better with other systems. For instance, if you use another reverse proxy such as CloudFlare to prevent DDoS attacks, it is better to hook it with NGINX rather than a Web Server that doesn’t have much experience with. 😃

--

--

Fatih Nayebi
Server Side Swift

Data Scientist, Author of Swift Functional Programming Book, Lecturer at McGill University