Install nginx on Windows, nginx download and installation tutorial

Many people who are familiar with Linux have used nginx. As a web server, nginx takes up little resources and has powerful functions. This small and powerful nginx is slowly eating up the market share of Apache and IIS, which are also web servers. So what powerful functions does nginx have? In addition to the web server function, many people may not know the real powerful functions of nginx.

Install nginx on Windows, nginx download and installation tutorial

How to install nginx on Windows

A lot of familiarityPeople have used, nginx as webThis small and powerful nginx is slowly eating up the web server.And IIS's share. So what powerful functions does nginx have? In addition to the functions of the web server, many people may not understand the real powerful functions of nginx.

nginx, Apache, and IIS are collectively known as the Three Musketeers of web servers.

nginx [engine x] is HTTP and reverseServer, Mailand generic TCP/UDP proxy servers, originally written byIgor SysoevFor a long time, it has been used in many heavily loadedRunning on the website, including YandexMail.RuVKand RamblerAccording to Netcraft, in April 2022, nginx served or proxied 21.79% of the busiest sites. Here are some success stories: DropboxNetflixWordPress.com, FastMail.FM.

Used for The nginx version for Windows uses the native Win32 API (not the Cygwin emulation layer). Currently only the select() and poll() (1.15.9) connection handling methods are used, so high performance and scalability should not be expected. Due to this and some other known issues, the nginx for Windows version is considered a beta version. Currently, it provides almost the same functionality as the UNIX version of nginx, except for XSLT filters, image filters, GeoIP module and embedded Perl language.

What is a reverse proxy?

Nginx can not only provide the basic functions of the web server, but also has excellent reverse proxy function. ) method refers to using a proxy server to acceptThe proxy server then forwards the request to the server on the internal network and returns the result obtained from the server to the client requesting the connection on the Internet. At this time, the proxy server appears to the outside world as a server.

Download nginx

First, visit the official website of nginx, where the Windows version of the nginx server software is provided.

Visit ngixn official website:https://nginx.org/

Install nginx on Windows, nginx download and installation tutorial

As of 2022, the latest version is nginx-1.21.6. Click Download on the right and download the Windows version of nginx on the download page.

Install nginx on Windows, nginx download and installation tutorial

You can choose to download the mainline version or the stable version.

Click to download:nginx/Windows-1.21.6 | nginx/Windows-1.20.2 

Install nginx

After downloading, double-click the nginx.exe file to run nginx.

Install nginx on Windows, nginx download and installation tutorial

After double-clicking nginx.exe, a black pop-up window flashes by, and nginx has been started successfully.

Or open a cmd command window, switch to the directory where you unzipped nginx, enter the command nginx.exe or start nginx, and press Enter to open nginx.

How to check whether nginx is started successfully? Enter in the browser http://localhost 

Install nginx on Windows, nginx download and installation tutorial

If you can see the nginx welcome page, congratulations, it has been started successfully.

Configure nginx

Nginx has been installed successfully. Next, you need to configure nginx. Like Linux, nginx in Windows also needs to modify the nginx configuration file to configure nginx.

Click to open CONF Folder, edit using notepad in the folder document

http{
server{
listen 80; # listening port, change here when configuring based on IP, such as 192.168.1.100:8080;
server_name boxpu.com;
# host domain name, if the actual project is released, fill in the domain name on the public network. If it is deployed locally, you can add the mapping of IP and domain name in the C:\Windows\System32\drivers\etc\hosts file
# mapping analysis, / represents the root path, and the analysis here also uses regular expression analysis.
location / {
root E:/uzbox; # website location path
index index.html index.htm; # home page file
}
}
}

When setting the directory where the website is located, use / instead of \.

Some common configurations in http{}

include mime.types; # file extension and file type mapping table
default_type application/octet-stream; # default file type
sendfile on; # turns on the efficient file transfer mode. The sendfile directive specifies whether nginx calls the sendfile function to output files. For ordinary applications, it is set to on. If it is used for downloading and other applications with heavy disk IO load, it can be set to off to balance the disk and network I/O processing speed and reduce the system load. Note: If the image does not display normally, change this to off.
autoindex on; # turns on directory listing access, suitable for download servers, closed by default.
tcp_nopush on; # prevents network congestion
tcp_nodelay on; # prevents network congestion
keepalive_timeout 120; # long connection timeout, in seconds
gzip on; # turns on gzip compression output

After the nginx.conf file is configured, use the following command to check whether the configuration file is correct.

nginx -t

After entering the command, if the configuration is correct, the message "syntax is ok" will be returned. If it is incorrect, the error line will be returned.

If the default port 80 is used, first check whether the port setting in the nginx.conf configuration file in the conf directory is 80. After confirmation, use the following command to check whether port 80 is available.

netstat -ano | findstr 0.0.0.0:80

Install nginx on Windows, nginx download and installation tutorial

Or use the following port detection command:

netstat -ano | findstr "80"

Install nginx on Windows, nginx download and installation tutorial

When you modify the nginx configuration file nginx.conf, you do not need to shut down and restart nginx. You only need to execute the following command to make the changes take effect.

nginx -s reload

If you want to shut down nginx

Enter the nginx command in the CMD window

nginx -s stop

or

nginx -s quit

nginx reverse proxy configuration

Add the tomcat_server block in the nginx.conf configuration file and fill in the domain name of the website that needs to be proxied in the tomcat_server block.

#upstream tomcat_server block reverse proxy settings.
upstream tomcat_server{
server t1.boxpu.com;
}

If you need to configure load balancing in the reverse proxy, you can configure multiple reverse proxy websites.

# load balancing website configuration core block
upstream tomcat_server{
server t1.boxpu.com weight=1;
server t2.boxpu.com weight=2;
}

The weight attribute indicates the weight of each server being accessed. The higher the weight, the higher the chance of being accessed. For example, if weight=2, nginx has priority access to t2.boxpu.com. When there are too many users accessing t2.boxpu.com, it will automatically switch to t1.boxpu.com.

After setting up the tomcat_server block above, set up the reverse proxy in the server block.

server {
location / {
proxy_pass http://tomcat_server;
#In the root directory, use proxy_pass to call the tomcat_server block.
}
}
After the configuration is complete, use nginx -t to check whether the syntax is correct, and finally restart nginx

nginx -s reload

Conclusion

Under WindowsIt has been successful. The configuration method under Windows is similar to that under Linux. There is no difference. The only problem is the difference in the root web storage directory. When calling the directory, use / instead of \.

nginx -s stop quick shutdown
nginx -s quit graceful shutdown
nginx -s reload changes the configuration, starts a new worker process with the new configuration, and gracefully shuts down the old worker process
nginx -s reopen to reopen the log file

Known Issues

Although several workers may be started, only one is actually doing any work.
The UDP proxy feature is not supported.

Possible future enhancements

Run as a service.
Use I/O completion ports as the connection handling method.
Use multiple worker threads in a single worker process.

score

Leave a Reply

Your email address will not be published. Required fields are marked *