Nginx is one of the most used for web server or reverse proxy in the internet and it is used because of its performance and it's simple to learn curve and finally its easy to setup. Let's see how to use it for windows
There isn't much articles on how to use Nginx in windows because Nginx is not made for windows and its performance comparing linux and windows in the same hardware is way worse.
So you should not use nginx in a production level website or app, if you use the concurrency of the nginx is not available in windows which means it's slow.
Keep that in mind, we can get going then.
In this article, we will cover from the download of nginx and running and editing the nginx configuration file and reload and stopping the nginx server from commandline.
If you want to learn more about nginx then join my course on Nginx 101 - The Complete Guide, it is a video based course with a cheatsheet and notes, available It is in the making right now once made I will update the link stay tuned.
Now I have made this complete article with GIF, If you don't like those for showing what we are doing then, mention in the comments I will stop using it.
So, let's setup everything and learn how to use nginx in windows,
Downloading and Running Nginx
First we have to download the nginx in our windows system, then we can get everything started,
Click here, this will take you to the official downloads page of nginx where you can download the files that are required
Then after that open your file explorer and make sure to extract the zip, and after extracting the zip open the folder, where all the contents are there
Then you have two ways to run the nginx server one is that you can click on the windows executable but if you use this then you will not be able to control the server like reloading and stuff
So we are going in the second way, click on the path in file explorer and type
cmd
Which will open a command line interface in the current directory and you can run
start nginx
This command will start the nginx server as expected and you can check that by going to the localhost,
Hurray, we have finally ran our nginx server let's configure our nginx configuration file.
Configuring nginx.conf
We have started our server and it is currently running let's configure our nginx to proxy the request coming to port 80 to port 3000,
Which is really simple to do, let's make it.
First open the directory in file explorer after that you could open any of your code editor in this tutorial I am using sublime, for the simplicity sake
You can also use VS Code, or Atom or anything you like. I used sublime here because everyone would have sublime,
So I opened sublime and opened the directory inside sublime and opened nginx.conf file through which you can configure nginx the way you want it.
Now delete everything inside the nginx.conf file and so that we could start fresh or else it's going to be so confusing.
events {}
http {
server {
listen 80;
location / {
proxy\_pass http://localhost:3000/;
}
}
}
In the first line we are mentioning events is set to an empty bracket it is necessary and you should set this.
In the second line we are setting up http block and inside that we are creating a server block and mentioning to port 80 and
We are again creating a location block and specifying that location to be / and when that is happened it will be proxied to the localhost port 3000
This should proxy pass the nginx to port 3000 from the port 80, which you could do to make your backend anonymous to the rest of the world.
Run this command for restarting the nginx server
nginx -s reload
Basics Commands For Nginx
Now that our server is working properly and we have configured nginx to load the port 3000 in the port 80.
If we have to reload our server again in the case that you had updated some contents of the file then you should run the previous command or the below command
nginx -s reload
Incase you want to stop your nginx server, then use the below code
nginx -s stop
If your nginx server is still up and running in localhost then you should again run the above code which will stop all the instance of nginx running in your windows machine.
Now if you want to start the server again use the below code.
start nginx
That's all for this article,
Conclusion
Using nginx in windows is discouraged because of its performant and some other issues on security but you can use it in case you are learning nginx.
And you are not familiar with linux that's completely okay most people never even heard of linux or they think that is some kind of ninja thing
But honestly it's not, nowadays linux's graphical interface has become way cooler than windows and even macOS.
The linux operating system has a little harder curve to learn but you could easily overcome that by using ubuntu or something as a VM and
Once you are familiar with ubuntu then switch it as your main os but also try out other linux distros and I love arch linux because of it's best performance and lesser background processes.
Okay that's all for this article guys, there are lots of cool content coming up in this blog. Just visit the blog whenever you have the free time or doubts or even want to learn new technology.