Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

The method of Windows nginx installation and configuration

2025-04-07 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)05/31 Report--

This article mainly explains the "method of Windows nginx installation and configuration". The content of the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "the method of Windows nginx installation and configuration".

What is nginx?

Nginx (engine x) is a lightweight web server, reverse proxy server and email (imap/pop3) proxy server.

What is a reverse proxy?

Reverse proxy (reverse proxy) means that the proxy server accepts the connection request on the internet, 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 behaves as a reverse proxy server.

You can refer to the example in the following figure:

Installation and use

Installation

Download address of nginx official website

The release version is divided into linux and windows.

You can also download the source code and run it after compilation.

Compile nginx from source code

After decompressing the source code, run the following command in the terminal:

. / configuremakesudo make install

By default, nginx is installed in / usr/local/nginx. You can change this setting by setting compilation options.

Windows installation

To install nginx/win32, you need to download it. Then extract it, and then run it. Let's take the root directory of disk c as an example:

Cd c:cd c:\ nginx-0.8.54 start nginx

Nginx/win32 runs in a console program rather than a windows service. The server mode is still in the process of development.

Use

Nginx is relatively simple to use, just a few commands.

The commonly used commands are as follows:

Nginx-s stop quickly shuts down nginx, may not save relevant information, and quickly terminates the web service.

Nginx-s quit closes nginx smoothly, saves relevant information, and ends the web service in a scheduled manner.

Nginx-s reload is overloaded because it changes the nginx-related configuration and needs to reload the configuration.

Nginx-s reopen reopens the log file.

Nginx-c filename specifies a configuration file for nginx instead of the default.

Nginx-t does not run, but only tests the configuration file. Nginx checks the syntax of the configuration file for correctness and attempts to open the file referenced in the configuration file.

Nginx-v displays the version of nginx.

Nginx-v displays the version of nginx, compiler version, and configuration parameters.

If you don't want to type the command every time, you can add a new startup batch file startup.bat in the nginx installation directory and double-click to run it. The contents are as follows:

@ echo offrem if nginx is started before startup and the pid file is recorded, kill specifies the process nginx.exe-s stoprem test configuration file syntax correctness nginx.exe-t-c conf/nginx.confrem displays version information nginx.exe-vrem launches nginxnginx.exe-c conf/nginx.conf according to the specified configuration

Actual combat of nginx configuration

I always think that it will be easier for people to understand the configuration of various development tools in combination with actual combat.

Http reverse proxy configuration

Let's first achieve a small goal: regardless of the complex configuration, just complete a http reverse proxy.

The nginx.conf configuration file is as follows:

Note: conf/nginx.conf is the default profile for nginx. You can also use nginx-c to specify your profile

# run the user # user somebody;# to start the process, which is usually set to equal the number of cpu 1 to worker_processes # the global error log error _ log dvure hand toolsAccording to nginxmuri 1.10.1 log d:/tools/nginx-1.10.1/logs/notice.log notice;error_log d:/tools/nginx-1.10.1/logs/info.log info. # pid file, which records the currently started nginx process idpid djar _ hand tools _ nginxmy _ 1.10.1 _ # set the maximum number of concurrent links for a single background worker process process} # set the http server and use its reverse proxy function to provide load balancing support http {# set mime type (mail support type). The type is defined by the mime.types file, include djub hand tools, nginxMAX 1.10.1, confme mime.types; default_type application/octet-stream # set log log_format main'[$remote_addr]-[$remote_user] [$time_local] "$request"'$status $body_bytes_sent "$http_referer"'"$http_user_agent"$http_x_forwarded_for"; access_log d:/tools/nginx-1.10.1/logs/access.log main; rewrite_log on The # sendfile instruction specifies whether nginx calls the sendfile function (zero copy mode) to output files. For ordinary applications, # must be set to on, and if it is used for downloading and other application disk io heavy-loaded applications, it can be set to off to balance the processing speed of disk and network iCando and reduce the uptime of the system. Sendfile on; # tcp_nopush on; # connection timeout keepalive_timeout 120; tcp_nodelay on; # gzip Compression switch # gzip on; # set the actual server list upstream zp_server1 {server 127.0.0.1 gzip 8089;} # http server server {# listens on port 80, which is a well-known port number for http protocol listen 80 # define using www.xx.com to access server_name www.helloworld.com; # homepage index index.html # point to the directory of webapp root d:\ 01_workspace\ project\ github\ zp\ springnotes\ spring-security\ spring-shiro\ src\ main\ webapp; # Encoding format charset utf-8; # proxy configuration parameter proxy_connect_timeout 180; proxy_send_timeout 180 Proxy_read_timeout 180; proxy_set_header host $host; proxy_set_header x-forwarder-for $remote_addr; # reverse proxy path (and upstream binding). Set the mapping path location / {proxy_pass http://zp_server1; after location } # static files. Nginx handles location ~ ^ / (images | javascript | css | flash | media | static) / {root d:\ 01_workspace\ project\ github\ zp\ springnotes\ spring-security\ src\ main\ webapp\ views; # expired for 30 days. Static files are not updated very often, and can be set to larger or smaller if updated frequently. Expires 30d;} # set the address location / nginxstatus {stub_status on; access_log on; auth_basic "nginxstatus" for viewing nginx status; auth_basic_user_file conf/htpasswd;} # prohibits access to .htxxx files location ~ /\ .ht {deny all } # error handling page (optional) # error_page 404 / 404.html; # error_page 500502 503504 / 50x.html; # location = / 50x.html {# root html; #}

All right, let's try it:

1. Start webapp and note that the port that starts the binding should be consistent with the port set by upstream in nginx.

two。 Change host: add a dns record to the host file in the c:\ windows\ system32\ drivers\ etc directory

127.0.0.1 www.helloworld.com

3. Start the startup.bat command in the previous article

4. Access to www.helloworld.com in a browser, and not surprisingly, you can already access it.

Load balancing configuration

In the previous example, the agent points to only one server.

However, in the actual operation of the website, most of the servers are running the same app, so you need to use load balancing to divert the traffic.

Nginx can also implement simple load balancing functions.

Suppose an application scenario: deploy the application on three servers in the linux environment: 192.168.1.11, 192.168.1.12, and 192.168.1.13, respectively. The domain name of the website is www.helloworld.com, and the public network ip is 192.168.1.11. Deploy nginx on the server where the public network ip resides, and load balance all requests.

The nginx.conf configuration is as follows:

Http {# sets the mime type, which is defined by the mime.type file include / etc/nginx/mime.types; default_type application/octet-stream; # sets the log format access_log / var/log/nginx/access.log; # sets the load balancer server list upstream load_balance_server {# weigth parameter indicates the weight, the higher the weight, the greater the probability of being assigned server 192.168.1.11 weight=5 80 Server 192.168.1.12 weight=6; 80 weight=1; server 192.168.1.13 weight=6;} # http server server {# listens on port 80 listen 80; # Definitions use www.xx.com access server_name www.helloworld.com; # to load balance request location / {root / root for all requests # define the default web site root location of the server index index.html index.htm; # define the name of the home index file proxy_pass http://load_balance_server; # request is directed to the list of servers defined by load_balance_server # here are some configuration of reverse proxies (optional) # proxy_redirect off; proxy_set_header host $host Proxy_set_header x-real-ip $remote_addr; # backend web server can obtain user real ip proxy_set_header x-forwarded-for $remote_addr; proxy_connect_timeout 90 through x-forwarded-for; # nginx connection timeout with backend server (proxy connection timeout) proxy_send_timeout 90 # backend server data return time (proxy send timeout) proxy_read_timeout 90; # response time of backend server (proxy receiving timeout) proxy_buffer_size 4k after a successful connection; # set the buffer size of proxy server (nginx) to store account information proxy_buffers 4 32k # proxy_buffers buffer, if the average page size is less than 32k, set proxy_busy_buffers_size 64k; # buffer size under high load (proxy_buffers*2) proxy_temp_file_write_size 64k; # set cache folder size, greater than this value, client_max_body_size 10m from the upstream server # maximum number of bytes per file requested by the client client_body_buffer_size 128k; # maximum number of bytes requested by the buffer proxy buffer client}

The website has multiple webapp configurations

When a website has more and more functions, it is often necessary to peel off some relatively independent modules and maintain them independently. In that case, usually, there will be multiple webapp.

For example: suppose the site has several webapp,finance (finance), product (product), admin (user center). Access to these applications is distinguished by context (context):

We know that the default port number of http is 80. If you start all three webapp applications on one server at the same time, you will not be able to use port 80. Therefore, these three applications need to bind different port numbers.

So, the problem is that when users actually visit the site, when they visit different webapp, they will not visit it with the corresponding port number. So, once again, you need to use a reverse proxy to do the processing.

Configuration is not difficult, so let's see how to do it:

Http {# some basic configuration upstream product_server {server www.helloworld.com:8081;} upstream admin_server {server www.helloworld.com:8082;} upstream finance_server {server www.helloworld.com:8083;} server {# some basic configuration is omitted here # points to product's server location / {proxy_pass http://product_server; by default } location / product/ {proxy_pass http://product_server;} location / admin/ {proxy_pass http://admin_server;} location / finance/ {proxy_pass http://finance_server;}

Https reverse proxy configuration

Some sites with high security requirements may use https, a secure http protocol that uses the ssl communication standard.

There are no popular science http protocols and ssl standards here. However, there are a few things you need to know to configure https with nginx:

The fixed port number of https is 443, which is different from port 80 of http

The ssl standard requires the introduction of a security certificate, so in nginx.conf you need to specify the certificate and its corresponding key

Everything else is basically the same as the http reverse proxy, except that the configuration in the server section is a little different.

# http server server {# listens on port 443. 443 is a well-known port number, mainly used for https protocol listen 443 ssl; # definition using www.xx.com to access server_name www.helloworld.com; # ssl certificate file location (common certificate file format is: crt/pem) ssl_certificate cert.pem; # ssl certificate key location ssl_certificate_key cert.key; # ssl configuration parameter (optional configuration) ssl_session_cache shared:ssl:1m Ssl_session_timeout 5m; # Digital signature, here using md5 ssl_ciphers highholl root; index index.html index.htm; ssl_prefer_server_ciphers on; location / {root / signature }} Thank you for your reading, the above is the content of "the method of Windows nginx installation and configuration". After the study of this article, I believe you have a deeper understanding of the method of Windows nginx installation and configuration, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report