Sketch:
Nginx is an open source Web server, and Nginx also provides Reverse proxy and load balancing Functions. -
Nginx is usually exposed as a load balancer to accept user requests in the external network, but also uses its reverse proxy function to forward user requests to the actual intranet server providing services.
What environment does Windows 10 need to install Nginx
It is suitable to install Nginx server on windows when the windows developer is used as a service to provide services to the outside world, only when the individual is tested locally or not used for commercial purposes.
Installation:
Download the compressed package:
http://nginx.org/en/download.html
Version 1.15.2: http://nginx.org/download/nginx-1.15.2.zip
After the download is completed, I unzip it to the location I want to place. Here I unzip it to D: Nginx (Chinese fields are not allowed in the directory path, and no spaces are recommended).
Start the Nginx server:
There are many ways to start nginx
(1) Double-click nginx.exe directly, and a black bullet window flashes by after double-clicking.
(2) Open the cmd command window, switch to the nginx decompression directory, enter the command nginx.exe or start nginx, and return.
Verify that the startup is successful and enter it in the address bar of the rover http://localhost:80, return. The following page shows the successful start-up.
The configuration file of ginx is nginx.conf in the conf directory. The default port of nginx listening is 80. If port 80 is occupied, it can be changed to an unoccupied port.
#user nobody; #nginx processes are generally set to the same number of cpu cores worker_processes 1; #Error log storage directory #error_log logs/error.log; #error_log logs/error.log notice; #error_log logs/error.log info; #pid logs/nginx.pid; #Working mode and upper limit of connection number events { worker_connections 1024; } http { include mime.types;#File extension and type mapping table default_type application/octet-stream;#Default file type #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 logs/access.log main; sendfile on; #tcp_nopush on; #keepalive_timeout 0; keepalive_timeout 65; #gzip on; #Virtual Host Based on Domain Name server { listen 80;#Listening port server_name localhost; #charset koi8-r; #access_log logs/host.access.log main; location / { root html; index index.html index.htm; } #error_page 404 /404.html; # redirect server error pages to the static page /50x.html # error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } # proxy the PHP scripts to Apache listening on 127.0.0.1:80 # #location ~ \.php$ { # proxy_pass http://127.0.0.1; #} # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000 # #location ~ \.php$ { # root html; # fastcgi_pass 127.0.0.1:9000; # fastcgi_index index.php; # fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name; # include fastcgi_params; #} # deny access to .htaccess files, if Apache's document root # concurs with nginx's one # #location ~ /\.ht { # deny all; #} } # another virtual host using mix of IP-, name-, and port-based configuration # #server { # listen 8000; # listen somename:8080; # server_name somename alias another.alias; # location / { # root html; # index index.html index.htm; # } #} # HTTPS server # #server { # listen 443 ssl; # server_name localhost; # ssl_certificate cert.pem; # ssl_certificate_key cert.key; # ssl_session_cache shared:SSL:1m; # ssl_session_timeout 5m; # ssl_ciphers HIGH:!aNULL:!MD5; # ssl_prefer_server_ciphers on; # location / { # root html; # index index.html index.htm; # } #} }
Our common configuration only needs to adjust the server node in nginx.conf.
The command to check whether port 80 is occupied is: netstat-ano | findstr 0.0.0:80 or netstat-ano | findstr "80"
When we modify the nginx configuration file nginx.conf, we do not need to close nginx and restart nginx, just execute the command nginx-s reload to make the change take effect.
For example, we can reserve it. We can configure reverse proxy and load balancing to append it directly.
- Examples of reverse proxy configuration
-
server { listen 80; #Listen on port 80 server_name blog.test.ken.io; #Monitored domain name location / { #Forwarding or processing proxy_pass https://ken.io; } error_page 500 502 503 504 /50x.html;#Error Page location = /50x.html { root /usr/share/nginx/html; } }
- Load balancing configuration example
- We can modify the nginx configuration file nginx.conf to jump to the specified server when accessing the nginx proxy server, that is, to configure the request forwarding address through proxy_pass, that is, when we still input http://localhost:80, the request will jump to the server we configure.
-
upstream serverswitch { server 127.0.0.1:8080; } server { listen 80; #Listen on port 80 server_name ss.test.ken.io; #Monitored domain name location / { #Forwarding or processing proxy_pass https://serverswitch; } error_page 500 502 503 504 /50x.html;#Error Page location / { proxy_pass http://xxx; } }
We can configure multiple target servers. When one server fails, nginx can automatically redirect requests to another server.
upstream serverswitch { server 127.0.0.1:8080 weight=2; server 127.0.0.1 weigth=1; } server { listen 80; #Listen on port 80 server_name ss.test.ken.io; #Monitored domain name location / { #Forwarding or processing proxy_pass https://serverswitch; } error_page 500 502 503 504 /50x.html;#Error Page location / { proxy_pass http://xxx; } }
A weight attribute is added above, which represents the weight that each server is accessed.
The higher the probability of being visited, the higher the probability of being visited.
-
nginx configures static resources
Place static resources (such as jpg|png|css|js, etc.) in the D:/nginx-1.12.2/static directory of the following configuration, and then configure them in the nginx configuration file as follows (note: static resource configuration can only be placed in location/location). Visit http://localhost:80/1.png in the browser to access the D:/nginx-1.12.2/static directory. 1. PNG pictures
Close nginx
If you use the cmd command window to start nginx, closing the cmd window cannot end the nginx process. There are two ways to close nginx
(1) Enter the nginx command nginx-s stop (quick stop nginx) or nginx-s quit (complete stop nginx) (2) Using taskkill taskkill/f/t/im nginx.exe
command | Explain |
nginx -h | View Help Information |
nginx -v | View the Nginx version |
nginx -s stop | Disabling Nginx |
nginx -s quit | Graceful deactivation of Nginx |
nginx -s reload | Reload the configuration and gracefully restart the process |
nginx -s reopen | Restart log files |