Nginx__Basic Introduction

Table of contents:

Advantages of Nginx

Detailed explanation of HTTP protocol

Nginx deployment - Yum

Nginx configuration file

Nginx compilation parameters

Nginx basic configuration

Nginx log Log

Nginx WEB module

Nginx access restrictions

Nginx access control


Advantages of Nginx

Nginx (engine x) is a high-performance HTTP (to solve the problem of C10k) and reverse proxy server, and also an IMAP/POP3/SMTP server.

1. As a web server, Nginx processes static files and index files, and the efficiency of automatic indexing is very high.

2. As a proxy server, Nginx can realize reverse proxy acceleration without caching and improve the running speed of the website.

3. As a load balancing server, Nginx can not only directly support Rails and PHP internally, but also support HTTP proxy server for external services, and also supports simple fault tolerance and load balancing using algorithms.

4. In terms of performance, Nginx is specially developed for performance optimization, and it pays great attention to efficiency in implementation. It adopts the kernel Poll model, can support more concurrent connections, can support the response to 50,000 concurrent connections at most, and only occupies very low memory resources.

5. In terms of stability, Nginx adopts staged resource allocation technology, which makes the CPU and memory usage very low. Nginx officials said that Nginx maintains 10,000 inactive connections, and these connections only occupy 2.5MB of memory. Therefore, attacks like DOS basically have no effect on Nginx.

6. In terms of high availability, Nginx supports hot deployment, and the startup speed is particularly fast. Therefore, the software version or configuration can be upgraded without interrupting the service. Even if it runs for several months, it does not need to be restarted, and it can be done almost 7x24 hours Run without interruption.

In short it is {

                High concurrency

                I/O multiplexing

                epoll

                asynchronous

                non-blocking

                }

Detailed explanation of HTTP protocol

HTTP--Hyper Text Transfer Protocol, hypertext transfer protocol, is a stateless connection established on TCP. The entire basic workflow is that the client sends an HTTP request, indicating the resource and requested action that the client wants to access After the server receives the request, the server starts to process the request, and takes corresponding actions to access server resources according to the request, and finally returns the result to the client by sending an HTTP response. The beginning of a request to the end of a response is called a transaction, and a log entry will be added to the server when a transaction ends.

Nginx deployment - Yum

Official link: http://www.nginx.org

1.yum install yum-utils

2.vim /etc/yum.repos.d/nginx.repo

3./etc/yum.repos.d/nginx.repo

[nginx-stable]
name=nginx stable repo
baseurl=http://nginx.org/packages/centos/$releasever/$basearch/
gpgcheck=1
enabled=1
gpgkey=https://nginx.org/keys/nginx_signing.key
module_hotfixes=true

[nginx-mainline]
name=nginx mainline repo
baseurl=http://nginx.org/packages/mainline/centos/$releasever/$basearch/
gpgcheck=1
enabled=0
gpgkey=https://nginx.org/keys/nginx_signing.key
module_hotfixes=true

4.yum-config-manager --enable nginx-mainline

5.yum -y install nginx

6.systemctl start nginx

7.systemctl enable nginx

8. Close the firewall systemctl stop firewalld

   Disable SELinux setenforce 0

9. View version nginx -V

10. Browser for testing

Nginx configuration file

View all configuration files rpm -ql   nginx

Common configuration files

/usr/share/nginx/html/index.html default home page

/var/log/nginx log folder

/var/cache/nginx cache folder

/etc/logrotate.d/nginx log rotation

/etc/nginx/nginx.conf General configuration file

/etc/nginx/conf.d sub-configuration file

/etc/nginx/conf.d/default.conf default website configuration file

/usr/lib64/nginx Nginx module directory

/etc/nginx/koi-utf character set, file encoding

/etc/nginx/mime.types file association program

/etc/nginx/modules third-party modules

/usr/lib/systemd/system/nginx-debug.servicenginx debugger startup script

Nginx compilation parameters

Nginx common compilation parameters

--prefix=/etc/nginx installation path

--sbin-path=/usr/sbin/nginx program files

--modules-path=/usr/lib64/nginx/modules module path

--conf-path=/etc/nginx/nginx.conf main configuration file

--error-log-path=/var/log/nginx/error.log error log

--http-log-path=/var/log/nginx/access.log access log

--pid-path=/var/run/nginx.pid program ID

--lock-path=/var/run/nginx.lock lock path to prevent repeated start of nginx

--http-client-body-temp-path=/var/cache/nginx/client_temp cache

--http-proxy-temp-path=/var/cache/nginx/proxy_temp proxy cache

--http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp php cache

--http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp python cache

--with-compat enable dynamic module compatibility

--user=nginx user

--group=nginx group

--with-http_auth_request_module authentication module

--with-http_dav_module add upload PUT, DELETE, MKCOL: create collection, COPY and MOVE methods) by default it is off

--with-http_flv_module NGINX adds MP4, FLV video support module

--with-http_gunzip_module compression module

--with-http_mp4_module multimedia module

--with-http_random_index_module nginx display random homepage module

--with-http_realip_module Nginx gets real IP module

--with-http_secure_link_module nginx secure download module

--with-http_slice_module nginx Chinese documentation

--with-http_ssl_module security module

--with-http_stub_status_module access status

--with-http_sub_module nginx replaces website response content

--with-mail mail client

cpu optimization parameters: {

                         --with-cc-opt='-O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -                        fexceptions -fstack-protector-strong --
                         param=ssp-buffer-size=4 -grecord-gcc-switches -m64 -mtune=generic -fPIC' 
                         --with-ld-opt='-Wl,-z,relro -Wl,-z,now -pie' 

                         }

Nginx basic configuration

Nginx configuration file address: /etc/nginx/nginx.conf

The nginx main configuration file (usually `nginx.conf`) is the core configuration file of the Nginx server, which contains the overall definition of the server's behavior. The following is the general structure and common directives of the nginx main configuration file:

1. `user`: Define the running user and user group of the nginx worker process.
2. `worker_processes`: Specifies the number of nginx worker processes, usually set to a multiple of the number of CPU cores.
3. `error_log`: Set the location and level of the error log.
4. `pid`: Specifies the location of the PID file of the nginx worker process.
5. `events`: configure nginx to process event modules and parameters, for example `worker_connections` is used to set the maximum number of connections for each worker process.
6. `http`: Define the configuration of nginx processing HTTP requests.
   - `include`: Import other configuration files.
   - `default_type`: Specifies the default MIME type.
   - `log_format`: Configure access log format.
   - `access_log`: Specifies the location and format of the access log.
   - `sendfile`: Configure whether to enable sendfile to transfer files.
   - `keepalive_timeout`: Specifies the timeout for client connections.
   - `gzip`: Configure the compression method of response data.
   - `server`: defines the configuration of the virtual host.
     - `listen`: Specify the port number and protocol to listen on.
     - `server_name`: Specifies the domain name of the virtual host.
     - `location`: Define the matching rules and corresponding processing logic of the request URL path.

The above is the general structure and some common instructions of the nginx main configuration file. In fact, the configuration of nginx is very flexible, and many other instructions and modules can be used to meet specific needs. You can customize and extend nginx's main configuration file according to your specific needs.

Virtual host configuration file address: /etc/nginx/conf.d/~~~~~

In Nginx, configuration files for virtual hosts are usually defined via the `server` block within the `http` block in the main nginx configuration file (`nginx.conf`). Each virtual host has a separate `server` block, which is used to specify the configuration of a specific domain name or listening port. Here is an example:

```
http {
  ...

  server {     listen 80;     server_name example.com;     root /path/to/root;     location / {       # The processing logic of this virtual host     }   }



    



  server {     listen 80;     server_name subdomain.example.com;     root /path/to/subdomain;     location / {       # Virtual hosts with different domain names can have different processing logic     }   }



    



  ...
}
```

In the above example, configurations for two virtual hosts are defined. The first virtual host uses `example.com` as the domain name, listens on port 80, and sets the requested root directory to `/path/to/root`. The second virtual host uses `subdomain.example.com` as the domain name, also listens on port 80, and sets the root directory of the request to `/path/to/subdomain`.

The `location` block can be used within the `server` block of each virtual host to define the processing logic for a specific URL path. Different actions such as proxy, cache, and redirection can be specified for each virtual host as needed.

You can add more virtual host configurations in the `http` block according to actual needs to support website services with different domain names or listening ports. At the same time, you can also use the `include` command to split the configuration file of the virtual host into separate files to improve the readability and maintainability of the configuration file.

Nginx log Log

Correct log: access_log

192.168.100.254 - - [17/Dec/2017:14:45:59 +0800] "GET /nginx-logo.png HTTP/1.1" 200 368 "http://192.168.100.10/" "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:57.0) Gecko/20100101 Firefox/57.0" "-"

  • The meaning of a log record

  • 192. Remote Host IP

  • - - user

  • 【2017】Time

  • get, download, and post submission.

  • /nginx-logo.png download image

  • http version

  • What is the result of the status code. right or wrong

  • 368 size

  • Which link is referenced from, the homepage http://192.168.100.10/

  • Mozilla 5.0 browser version

  • Windows NT client system type

  • - remote client host address (see note)

Error log: error_log

Nginx WEB module

Connection Status

random home page

replace module

file read

file compression

page cache

Anti-leech

Nginx access restrictions

ngx_http_limit_req_module request frequency limit

ngx_http_limit_conn_module connection frequency limit

Nginx access control

Guess you like

Origin blog.csdn.net/SongLiang02_/article/details/132677502