http protocol and httpd WEB application's chat

  What is the web? In everyday life we ​​often hear the word web, which in the end what is it? Today we chat web application http protocol; now believe that life in the Internet age we are, http agreement, it is for us is not very familiar with it! For example, two-eleven didodecanyl we go to a treasure, an East to buy merchandise, behind it is to provide services to us in a pile of web server via http or https protocol agreement; the simple point that web service is a C / S architecture, server to provide services to clients accessed via http or https protocol agreement, get the resources corresponding to the server, which is a web service; the so-called C / S architecture is the client and server architecture; for the server side of the iso seven the meaning of existence layer network model application layer protocol is to achieve a specific application, under normal circumstances they will register a well-known service ports to the kernel space and listening on an address, or addresses, external services; this also features software services usually work in C / S architecture; and http protocol on port 80 is working to provide services outside of the application layer; for there is no client-side monitor this to say, the client is usually at work the application layer or we can interpret it this way, it is not actually working at the application layer; we know that network resources only for a computer kernel Rights Directive to be able to operate, but for the average user, the core functionality we need to provide an application in user space, system calls through an API application to kernel in order to achieve the ability to operate network resources; implementation of the client software there are many common graphical interface of the browser chrome Yeah, firefox Yeah, ie, etc., these programs are web services client, in addition of course, as there are characters in the web interface of the Linux client, wget, curl , elinks like; these user space clients to access the server, a random port up to the kernel, then his own request information header by encapsulating the application layer, transport layer, network layer, data link layer, physical layer, after the last sent by the card to the server, the server receives the client's request packet will client package, removal of the layers, so that to get the client's request, a request to get the client, and then encapsulated response packet encapsulation response similar processes packets encapsulated client request message, the first encapsulating the application layer header and the transport layer, network layer, Data link layer, a physical layer and then transmitted to the client; the client to get the packets, decapsulates the layers, the final response to a service side

  Note: The above process is an Ethernet packet encapsulation and decapsulation of packets, HTTP protocol is an application layer protocol, the application layer is typically the case where the above three; application layer works in user space, and the lower four work in kernel space, we usually put the following four sub-network communications layer called; kernel space function and this is our previous iptables which must be frequently mentioned kernel system calls initiated by the user-space program to be able to operate in kernel space function, like iptables, ipvsadm such tools we do not call them calls, but the cause of the tool; the same way as the web client we usually just call it is a tool, rather than call service; service is usually called the kernel needs to register a port and then listens on a particular address, we put this work in user space (or functional kernel space) program calls, simple point of speaking services need to be monitored, the reason should be monitored because of the need for communication;

  One of them the most common; IPC (Inter-Process Communication, inter-process communication), the so-called inter-process communication are two processes to exchange data with each other; there are many ways inter-process communication, such as shared memory, memory mapping, BSD socket etc. BSD socket, which allows in a different host (same host is also possible of course, but the same host this socket mechanism becomes less efficient) to communicate between processes on;? What socket believe understand the point of network programming people heard the word it! IP socket is applied a so-called port, TCP socket for a socket can be divided, udp socket, raw sockets, which are usually the case by the API socket (socket communication related encapsulates kernel system call) to create a different socket types; SOCK_STREAM is expressed as a TCP socket, SOCK_DGRAM says UDP socket, SOCK_RAW it means raw socket; socket address format used, we can be divided into the IPV4 socket (between different processes on the same host address is based on a communication socket socket used), and a socket ipv6 unix_sock; ipv4 e.g. AF_INET format represented on the socket, it means the AF_INET6 socket format ipv6 , AF_UNIX says unix_sock socket format; two of which for network communication, usually located in different inter-process communication host, while the latter two processes communicate unix_sock for the same host is more common; the latter is our common sock documents, such as the mysql client is connected to a local server using this mechanism;

  Understand the above principle underlying communication, let's talk about today's topic http it

  http is hyper text transfer protocol shorthand, it is an application layer protocol, the default port tcp protocol at work 80, belongs to a text protocol; in version 0.9 of the http protocol, it can only be used to transmit text html, also used on the Internet the most primitive version, quite simple; the introduction of version 1.0 of the cache mechanism, MIME mechanisms and more method, so that all of a sudden http protocol is widely used, MIME is the multipurpose Internet Mail extensions type, its main role is to let some Annex non-text formats can be transmitted via the Internet to each other, and can be reduced to the original format of the attachment mechanism after the recipient receives, what does that mean? It allows some accessories non-text format reduced to a mechanism for attachment of the original format after transmission through the Internet; http protocol which makes it possible to transmit data format other than text, such as pictures, videos, music and so on; the so-called method is the client server resource requests data transmission method request, in version 0.9 only supports GET http protocol this kind of method, in version 1.0 http protocol support more request method, such as GET, PUT, POST, DELETE, HEAD, etc. ; after that version 1.1, it is mainly enhanced caching capabilities in version 1.0; 2.0, 2.0 is then mainly in the 1.1 version done a lot of optimization, making many of the original edition 1.1 performance issues are resolved, now http protocol version used is the mainstream version 1.1 and 2.0;

  http operating mode

  Http mode of operation is very simple, we have mentioned above, is nothing more than a client request, the server response; where we request packet called a http request response message called the http response; for the client's first request and service response response time we put this process is called a transaction http protocol ends;

  web resources: web resource

  web resource points static and dynamic resources, the so-called static resource is the server without having to do extra processing resources, we call static resources, what does that mean? Client requests resources on the server is what is what, we do not need to put the resources of this server is called a static additional processing resources through the http protocol transmission to the client, the common static resources .jpg, .png, .gif, .html, txt, .js, .css, .mp3, .avi like; called dynamic resource on the contrary, the client requests a resource is required to be the program execution or processing, then execute results after treatment or sent to the client, we need to put this server through the resources to deal with the implementation of a program called dynamic resource; common are .php.jsp.asp etc; also point to note here is that, we usually see page resources are often not a single resource, it may have a lot of resources composed of dynamic, but also static, requires a separate request for each resource as follows;

  Tip: You can see that we visit a page behind the fact is there are a lot of resources form;

  Resource identification mechanisms: URL

  The so-called resource identifier that is used to describe the location of a particular resource server; simple point is that resources for the client to tell the server to be accessed; usually add a URL Protocol ": //" plus server address (domain name may be, or host name) [: port] (if the server side work in a non-standard port, we also need to specify the port) and then add the resource path or resource name (resource path here refers to the root path of web services start, rather than the file system root path) such as http://www.test.com/index.html this is a URL, the client involved in a URL corresponding to the server will know the resources requested by the client is on this host www.test.com index.html file, the server will respond to the corresponding file in the directory to the client; for the server how to find it on its own file system index.html file, usually by the end of our definition of service documentroot determined in nginx we can specify the root directory of a corresponding host virtual file system path by mapping an instruction root;

  A complete http request processing usually have to complete the following steps:

  (1) establish a connection or processing: receiving a request or deny the request;
  (2) receiving a request: receiving from a host on the network a request message during a request for a particular resource;

  response model http response model http mainly single-process I / O model, multi-process, multiplexed I / O model and taking multi-process model, where a single-process I / O model is to start a process to handle user requests it means, can only handle a request, a plurality of serially in response to the request; multi-process I / O model represents a plurality of sub-processes from the promoter parent process in parallel, each sub-process a user request accordingly; multiplexing the single-process I / O model has two modes, multi-threading mode and event-driven mode, the former represents a process to generate N threads, one thread to handle a request; and event-driven mode in a process to deal directly with N requests, no thread of argument; multiplexing and more process I / O model, a plurality of starting processes, each generate n threads, each process a request, the number of requests is the number of threads in response to the number of processes generated by each process;
  (3) processing request: server and the resource request information related to a method of analyzing the request packet, and acquisition request, in accordance with methods, resources, optional header and a body portion into the request Row

    Metadata: a request packet header
    <Method> <the URL> <VERSION>
    HEADERS format name: value
    <body Request>

      ……

    Example:

  After the server access to resources web server request message requests that the server storing web resources, is responsible for providing a static resource request of the other party to the requestor, or dynamic operation; resource acquisition request message request: (4) access to resources generated resource; resources placed in the path of a particular local file system: DocRoot

    Example: If we map the root directory of web resources into / var / www / html file on the system, namely www.test.com/ ---- >> / var / www / html, user access www.test.com/ index.html, equivalent /var/www/html/index.html access the file system on the server resources, the web path mapping mode typically there are four, the first one is specified by defining the docroot, a second alias alias is specified, the third is the fourth docroot virtual host is the user's home directory docroot

  (5) constructing a response message: Once the Web server identification from resources on the implementation of the operation method described in the request, and returns a response packet. Response message includes response status code, the response header, if it is generated in response to the body, the body further comprises a response;

    Responding entity: if the transaction had a response body, it will be content in the response packet sent back in the past. Response packet generally comprises: describes the MIME type of the response body Content-Type header, describes the main content of the length of the body in response Content-LengthContent-Type header packets and actual

    Example:

  (6) transmits a response message: Web Server will face the same problem with the reception data transmitted connection data. The server may have many pieces to each client connection, some free, some sending data to the server, and some in response to the client to send data back end. To record the state of the server connection, special attention to treatment of persistent connections. After the non-persistent connection to, the server should send the entire message, close your end of the connection. For persistent connections, the connection may remain open, in this case, the server to correctly calculate the header Content-Length, otherwise the client can not know what the response to the end of time;
  (7) log: Finally, when at the end of the transaction, Web server adds an entry in the log file to describe the transaction was executed;

   HTTP server applications

  http server program have httpd, nginx, lighttpd nginx and other related use and instructions can refer back to my blog https://www.cnblogs.com/qiuhom-1874/category/1646010.html ; then we focus on it said httpd;

  httpd is the early 1990s, the National Center for Supercomputing Applications NCSA development, the open source community in 1995 published apache (a patchy server) it is a highly modular, supports dynamic loading and unloading modules to support multiple processing modules, so-called multi-channel the processing module is three httpd response model;

  The first is prefork multi-process model, each process in response to a request, a main process, the main process responsible for the generation process and sub-process recovery of the child, socket creation and receive requests and distributed to a child process handling; n sub-processes (a process a request to generate a thread in the sub-process), the child process is primarily used for processing the request; working model: generate several idle process will advance, in response to a user request at any time to wait; the maximum and minimum idle idle;

  The second is the worker multi-process multi-threaded model, in this model, each thread handling a user request; it will be the responsibility of the same main process generates a child process, socket creation and reception of the request and dispatch processed to a child process; n-th process, and the above profork except that, where the sub-process responsible for generating a plurality of threads, each thread processing a request; Thus in this model is complicated by the number of response n sub-process take each child process generated M thread;

  The third is event: event-driven model, multi-process model, a primary process to generate m sub processes, each of n directly in response to the request, and transmits the response request: m * n, a dedicated thread to manage these keep-alive type thread, when there is a real request, the request is passed to the service thread, after the implementation, but also allowed the release. This enhances the request processing capability at high concurrency scenarios

The above is httpd some brief, in a subsequent article I will continue to update and other usage instructions httpd configuration instructions, have interested friends can focus attention, to discuss;

Guess you like

Origin www.cnblogs.com/qiuhom-1874/p/12515075.html