Installing Nginx on Windows — A Step-by-Step Guide
Are you looking to set up Nginx on your Windows machine for web development or testing purposes?
Nginx is a powerful web server and reverse proxy that can be a valuable addition to your development toolkit. This step-by-step guide will walk you through the process of installing Nginx on your Windows environment, along with generating SSL/TLS certificates for secure connections. Whether you’re a seasoned developer or just getting started, we’ll cover the essentials to help you get up and running. Let’s dive into the installation process.
Step 1: Download Nginx for Windows
Visit the official Nginx website to download the latest version of Nginx for Windows: Nginx Download.
Once the download is complete, extract the contents of the archive to a directory of your choice.
Step 2: Generate an SSL/TLS Certificate
If you don't have OpenSSL in your system install it. Visit the OpenSSL website to download the latest version of OpenSSL for Windows: OpenSSL Download.
If you already have OpenSSL installed on your system, you can use the following command to generate an SSL/TLS certificate and key. Otherwise, you’ll need to install OpenSSL.
Run the command below to generate a self-signed certificate and key:
openssl req -x509 -nodes -newkey rsa:2048 -keyout C:\nginx-1.24.0\conf\cert\localhost.key -out C:\nginx-1.24.0\conf\cert\localhost.crt -days 365
After running the command, you will be prompted to provide some information:
Country Name (2 letter code): IN
State or Province Name: TN
Locality Name: Your city name
Organization Name: Your organization
Organizational Unit Name: localhost
Common Name: localhost
Email Address: Your email address
This command will generate the localhost.key and localhost.crt files in the specified directory.
Move the cert folder to the root of your C:/ drive, or any other location, but make sure it’s not placed inside the nginx-1.24.0 folder.
Step 3: Configure Nginx
Open the C:\nginx-1.24.0\conf\nginx.conf file and add the following code:
server {
listen 8000 ssl;
listen [::]:8000 ssl;
root /var/www/html;
index index.html index.htm index.nginx-debian.html;
server_name localhost;
ssl_certificate C:\cert\localhost.crt;
ssl_certificate_key C:\cert\localhost.key;
ssl_session_cache shared:SSL:1m;
ssl_session_timeout 5m;
ssl_ciphers HIGH:!aNULL:!MD5;
ssl_prefer_server_ciphers on;
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_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_pass http://localhost:3000/;
}
}
Step 4: Verify Nginx Configuration
Open a command prompt and navigate to the C:\nginx-1.24.0 directory. To test the Nginx configuration, run the following command:
nginx -t
You should see the following output:
nginx: the configuration file C:\nginx-1.24.0/conf/nginx.conf syntax is ok
nginx: configuration file C:\nginx-1.24.0/conf/nginx.conf test is successful
Step 5: Start Nginx
To start the Nginx application, run the following command:
nginx
After Nginx starts, you can access your application at both HTTP (http://localhost:3000/) and HTTPS (https://localhost:8000/).
In summary, installing Nginx on Windows empowers web developers and system administrators with a versatile web server and enhanced security through SSL/TLS certificates. Follow the provided guide to quickly integrate Nginx into your development and testing environment. Remember, the Nginx community and online resources are ready to assist with any questions or challenges. Enjoy the benefits of Nginx for seamless web development. Happy coding!
Connect with me on LinkedIn too!