Nginx installation and deployment tutorial in Windows11 environment

1. Introduction to Nginx

  • Nginx (engine x) is a high-performance HTTP and reverse proxy server, as well as an IMAP/POP3/SMTP server.
  • Nginx is a lightweight web server that can be used as an independent server to deploy websites (similar to Tomcat). It is widely used, especially when the front and back ends are separated. Its high performance and low memory consumption structure is favored by many large companies. Favored, such as Taobao website erection.
  • Nginx was developed by Igor Sysoyev for the second most visited Rambler.ru site in Russia (Russian: Рамблер)

Two, the difference between Nginx and Tomcat

Generally speaking, Apache or Nginx is an HTTP Server, and Tomcat is an Application Server or some people say it is a Web Server, but in essence it is a container for Servlet/JSP applications.
An HTTP Server is concerned with transmission and access control at the HTTP protocol level, so you can see functions such as proxy and load balancing on Apache/Nginx. The client accesses the resources (HTML files, image files, etc.) stored on the server through the HTTP Server. Through CGI technology, the processed content can also be distributed through HTTP Server, but an HTTP Server only transmits the files on the server to the client through HTTP protocol faithfully.
insert image description here

The application server, as the name implies, is an application container. It first needs to support the runtime environment of the development language (for Tomcat, it is Java) to ensure that the application can run normally on the application server. Second, it needs to support application-related specifications, such as class libraries and security features. For Tomcat, it is necessary to provide the standard class library, Interface, etc. needed for JSP/Sevlet operation. For convenience, the application server often also integrates the functions of HTTP Server, but it is not as powerful as the professional HTTP Server, so the application server often runs behind the HTTP Server, executes the application, converts the dynamic content into static content, and passes HTTP Server distributes to clients.
insert image description here

This is why Nginx is often used in conjunction with Tomcat:

  • Separation of dynamic and static resources : use Nginx's reverse proxy function to distribute requests: all dynamic resource requests are handed over to Tomcat, while static resource requests (such as pictures, videos, CSS, JavaScript files, etc.) are directly returned to the browser by Nginx, This can greatly reduce the pressure on Tomcat.
  • Load balancing : When the business pressure increases, one Tomcat instance may not be enough to handle it, then multiple Tomcat instances can be started for horizontal expansion, and Nginx's load balancing function can distribute requests to different instances through algorithms deal with

Three, Nginx installation and use

3.1 download

In the development process, we often need to use Nginx as the web server under the window system.
download link:

There are two versions of Linux and Windows.
insert image description here
After clicking, it will be downloaded. After the download is complete, the installation will start. In fact, the official website has already told how to install. There are detailed instructions in "documentation -> nginx windows" on the right, only in English
insert image description here

Those who can't read English can consider the Chinese website, the location is the same.

3.2 Installation and configuration

Download it locally and unzip it directly
insert image description here
insert image description here

  • conf: the directory where the Nginx configuration file exists
  • docs: the directory where Nginx documents are stored
  • html: directory for storing static html files
  • logs: the directory where Nginx logs are stored
  • temp: directory for storing temporary files

The default port number of Nginx is 80. If port 80 is occupied, we need to modify it in the nginx.conf file under the conf directory: open nginx.conf and configure it
insert image description here
insert image description here
according to your own needs. Some simple routine adjustments are listed below. Optimal configuration:

 #user  nobody;
 
#==工作进程数,一般设置为cpu核心数
worker_processes  1;
 
#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;
 
#pid        logs/nginx.pid;
 
 
events {
    
    
 
    #==最大连接数,一般设置为cpu*2048
    worker_connections  1024;
}
 
 
http {
    
    
    include       mime.types;
    default_type  application/octet-stream;
 
    #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;
 
    #当配置多个server节点时,默认server names的缓存区大小就不够了,需要手动设置大一点
    server_names_hash_bucket_size 512;
 
    #server表示虚拟主机可以理解为一个站点,可以配置多个server节点搭建多个站点
    #每一个请求进来确定使用哪个server由server_name确定
    server {
    
    
        #站点监听端口
        listen       8800;
        #站点访问域名
        server_name  localhost;
        
        #编码格式,避免url参数乱码
        charset utf-8;
 
        #access_log  logs/host.access.log  main;
 
        #location用来匹配同一域名下多个URI的访问规则
        #比如动态资源如何跳转,静态资源如何跳转等
        #location后面跟着的/代表匹配规则
        location / {
    
    
            #站点根目录,可以是相对路径,也可以使绝对路径
            root   html;
            #默认主页
            index  index.html index.htm;
            
            #转发后端站点地址,一般用于做软负载,轮询后端服务器
            #proxy_pass http://10.11.12.237:8080;
 
            #拒绝请求,返回403,一般用于某些目录禁止访问
            #deny all;
            
            #允许请求
            #allow all;
            
            add_header 'Access-Control-Allow-Origin' '*';
            add_header 'Access-Control-Allow-Credentials' 'true';
            add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
            add_header 'Access-Control-Allow-Headers' 'DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type';
            #重新定义或者添加发往后端服务器的请求头
            #给请求头中添加客户请求主机名
            proxy_set_header Host $host;
            #给请求头中添加客户端IP
            proxy_set_header X-Real-IP $remote_addr;
            #将$remote_addr变量值添加在客户端“X-Forwarded-For”请求头的后面,并以逗号分隔。 如果客户端请求未携带“X-Forwarded-For”请求头,$proxy_add_x_forwarded_for变量值将与$remote_addr变量相同  
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            #给请求头中添加客户端的Cookie
            proxy_set_header Cookie $http_cookie;
            #将使用代理服务器的主域名和端口号来替换。如果端口是80,可以不加。
            proxy_redirect off;
            
            #浏览器对 Cookie 有很多限制,如果 Cookie 的 Domain 部分与当前页面的 Domain 不匹配就无法写入。
            #所以如果请求 A 域名,服务器 proxy_pass 到 B 域名,然后 B 服务器输出 Domian=B 的 Cookie,
            #前端的页面依然停留在 A 域名上,于是浏览器就无法将 Cookie 写入。
            
           #不仅是域名,浏览器对 Path 也有限制。我们经常会 proxy_pass 到目标服务器的某个 Path 下,
            #不把这个 Path 暴露给浏览器。这时候如果目标服务器的 Cookie 写死了 Path 也会出现 Cookie 无法写入的问题。
            
            #设置“Set-Cookie”响应头中的domain属性的替换文本,其值可以为一个字符串、正则表达式的模式或一个引用的变量
            #转发后端服务器如果需要Cookie则需要将cookie domain也进行转换,否则前端域名与后端域名不一致cookie就会无法存取
        #配置规则:proxy_cookie_domain serverDomain(后端服务器域) nginxDomain(nginx服务器域)
            proxy_cookie_domain localhost .testcaigou800.com;
            
            #取消当前配置级别的所有proxy_cookie_domain指令
            #proxy_cookie_domain off;
            #与后端服务器建立连接的超时时间。一般不可能大于75秒;
            proxy_connect_timeout 30;
        }
 
        #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;
        }
 
    }
    
  #当需要对同一端口监听多个域名时,使用如下配置,端口相同域名不同,server_name也可以使用正则进行配置
  #但要注意server过多需要手动扩大server_names_hash_bucket_size缓存区大小
  server {
    
    
    listen 80;
    server_name www.abc.com;
    charset utf-8;
    location / {
    
    
      proxy_pass http://localhost:10001;
    }
  }
  server {
    
    
    listen 80;
    server_name aaa.abc.com;
    charset utf-8;
    location / {
    
    
      proxy_pass http://localhost:20002;
    }
  }
}

3.3 How to start Nginx

  • Start via the command line (the method recommended by the online tutorial),
    enter cmd in the box of the absolute path of the nginx installation directory, and press Enter.
    insert image description here
    Enter the command in the command line window start nginx, click Enter to start,
    insert image description here
    and then enter localhost:80 in the browser address bar. Then press Enter (80 port number can be omitted, even if it is entered, it will not be displayed), and the following page will be displayed, which means the startup is successful.
    insert image description here

  • Double-click Nginx.exe to start
    Double-click the nginx.exe application in the nginx directory. Generally, a black pop-up window will flash, which means the startup is successful.
    insert image description here

In order to ensure that it is really started, you can check whether there is an Nginx startup item in the task management
insert image description here
, then enter localhost:80 in the browser address bar, and then press Enter, if you can see the following page, then nginx started successfully.
insert image description here

3.4 Failed to start

If the startup fails, please check the startup error log: error.log is the log file under the logs folder in the nginx directory.
insert image description here

Common mistakes:

  • Port number is occupied
  • The nginx folder path contains Chinese.
    For other errors, see the description in the log for details.

Guess you like

Origin blog.csdn.net/qq_39335404/article/details/131780527