Nginx personal summary

nginx concise tutorial

installation

You can use apt-get to install nginx or download the installation package from the official website ( http://nginx.org/download ) to install it yourself. However, the final installation path of the two is different, so the file path that needs to be configured later is also different. The specific differences are as follows:

Installation by downloading the installation package (nginx-1.9.9.tar.gz), the software path is under /usr/local/nginx

installation method:

  1. wget http://nginx.org/download/nginx-1.9.9.tar.gz
  2. tar -zxvf nginx-1.9.9.tar.gz
  3. cd nginx-1.9.9/
  4. ./configure: At this time, if it may prompt such as lack of PCRE Library, just pass sudo apt-get install libpcre3 libpcre3-dev
  5. make
  6. sudo make install

Structure introduction: 

  • logs/ (log path): Contains access.log (normal access log), error.log (error log), nginx.pid (nginx id number at runtime)
  • sbin/ (execution path): contains nginx (executable file)
  • html/ (static content path): index.html (the default nginx welcome page, which will be introduced in the configuration later)
  • conf/ (configuration path): nginx.conf (configuration file)

Installation through apt-get, the software path is under /etc/nginx

  • The executable file has been added to /usr/sbin/nginx and can be executed directly on the command line
  • conf.d/: place a customized configuration file
  • sites-available/ sites-enabled/: sites-available stores system configuration files, and sites-enabled stores connections for certain configurations that need to be used in sites-available
  • nginx.conf: nginx configuration file, which includes the configuration in conf.d/ and sites-enabled/ above

Configuration

The things that need to be configured for the two installation methods are roughly similar, but there are still some differences in details.

Install via installation package

The configuration required is mainly the /usr/local/nginx/conf/nginx.conf file, the main structure of which is as follows

http {
     # 静态文件映射
     server {
         listen          80;       ------------- 监听的端口
         server_name     www.domain1.com;  ------------- 直接访问的 url
         location / {  ------------- 映射的 url 路径,后续路径将对应路径与 root 指定的根路径的对应路径关联
             index index.html;    ------------- 映射的对应文件
             root  /var/www/domain1.com/htdocs;   ------------- 实际访问的根目录
         }
     }
 
     # 动态访问映射
     server {
         listen          80;
         server_name     www.domain2.com;
         location /test {
             proxy_pass http://127.0.0.1:8051/real_test ------------- 对 www.domain2.com/test 的访问将被转发到本机 8051端口的 real_test 请求上
         }
     }
}

Its structure mainly uses http {} to specify http related content, and server {} specifies each mapping relationship. If you need load balancing (there are multiple services), you can configure it by configuring upstream {}. It should be noted that during the mapping, whether the location /*** path needs to be added at the end/The end needs to be consistent with the path associated with the actual service. If the actual service has it, add /; if there is no, don't add it.

Install via apt-get

The main function of installing /etc/nginx/nginx.conf through the system is to introduce the configuration under the two folders conf.d/ and sites-enabled/.

The specific configuration and installation package are similar and will not be introduced. However, there are a few things to note:

  • Due to the difference in the order of introduction, it seems that the configuration of sites-enabled/ will overwrite conf.d/; at the same time, the overall monitoring of port 80 in sites-enabled/default may cause the configuration in conf.d/ to be invalid.
  • Since the include operation in /etc/nginx/nginx.conf is already in http {}, the configuration in conf.d/ and sites-enabled/ should start directly from server {}.
server {
    listen 80;
    server_name 127.0.0.1;

    location / {
        proxy_pass http://127.0.0.1:8051/api;
    }
}

Operation command

  • nginx -v: View version information
  • nginx -t: Check whether the current configuration is legal
  • nginx: start nginx
  • nginx -s reload|reopen|stop|quit: reload configuration|restart|stop|quit nginx

 

Guess you like

Origin blog.csdn.net/a40850273/article/details/107106613