In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)05/31 Report--
This article mainly explains "how to configure Web server with Nginx". Interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn "how to configure Web server with Nginx".
1. Set up a virtual server
The NGINX profile must contain at least one server directive to define the virtual server. When NGINX processes a request, it first selects the virtual server that provides the request.
Virtual servers are defined by server directives in the http context, for example:
Http {server {# Server configuration}}
Multiple server instructions can be added to the http context to define multiple virtual servers.
The server configuration block typically includes a listen instruction that specifies the IP address and port (or Unix domain socket and path) on which the server listens for requests. Both IPv4 and IPv6 addresses are accepted; square brackets (.
The following example shows the configuration of a server that listens on IP address 127.0.0.1 and port 8080:
Server {listen 127.0.0.1 The rest of server configuration 8080; # The rest of server configuration}
If the port is omitted, the standard port is used. Similarly, if one address is omitted, the server listens for all addresses. If the listen directive is not included, the "standard" port is 80/tcp and the "default" port is 8000/tcp, depending on superuser permissions.
If more than one server matches the requested IP address and port, NGINX tests the requested host header domain according to the server_name instruction in the server block. The arguments to server_name can be full (exact) names, wildcards, or regular expressions. A wildcard is a string that begins, ends, or both contains an asterisk (*); an asterisk matches any sequence of characters. NGINX uses Perl syntax for regular expressions; use a tilde () before them. This example illustrates an exact name.
Server {listen 80; server_name example.org www.example.org;.}
two。 Configuration location
NGINX can send traffic or provide different files to different agents based on the request URI. These blocks are defined using the location instruction placed in the server instruction.
For example, you can define three location blocks to instruct the virtual server to send some requests to one proxy server, send other requests to different proxy servers, and provide the rest by passing files from the local file system.
The NGINX test requests the URI based on the parameters of all location instructions and applies the instructions defined in the matching location. Within each location block, it is usually possible (with some exceptions) to place more location instructions to further refine the processing of specific group requests.
Note: in this tutorial article, the word location refers to a single location context.
The location instruction has two types of parameters: a prefix string (pathname) and a regular expression. For the request URI to match the prefix string, you must start with the prefix string.
The following example location with the pathname parameter matches a request URI that starts with / some/path/, such as / some/path/document.html, which does not match / my-site/some/path because / some/path does not appear at the beginning of the URI.
Location / some/path/ {...}
The regular expression used to be a case-sensitive waveform symbol (~), or a case-insensitive waveform symbol (~ *). The following example matches the URI containing the string .html or .html to any location.
Location ~\ .html? {.}
To find the location that best matches the URI, NGINX first compares the URI to the position of the prefix string. Then use regular expressions to search for locations.
The location context can contain instructions that define how to parse the request-the server that provides the static file or passes the request to the agent. In the following example, a request that matches the first location context provides a file from the / data/images directory and passes the request that matches the second location to the proxy server that hosts the www.example.com domain content.
Server {location / images/ {root / data;} location / {proxy_pass http://www.example.com;}}
The root directive specifies the file system path in which to search for static files to be provided. The request URI associated with the location is appended to the path to get the full name of the static file to be provided. In the above example, in response to the request for / images/logo.png, the NGINX provides the actual local corresponding file to the server is: / data/images/logo.png.
The proxy_pass directive passes the request to the URL access proxy server that uses the configuration. The response from the proxy server is then passed back to the client. In the above example, all requests for URI that do not start with / images/ are passed to the proxy server (that is, www.example.com).
3. Use variables
You can use variables in the configuration file to make the requests of the NGINX process different according to the circumstances defined. A variable is a named value calculated at run time and used as an argument to an instruction. A variable is represented by the $(dollar) sign at the beginning of its name. Variables define information based on the state of the NGINX, such as the properties of the request being processed.
There are many predefined variables, such as core HTTP variables, and you can use the set,map and geo directives to define custom variables. Most variables are calculated at run time and contain information related to a particular request. For example, $remote_addr contains the client IP address, and $uri holds the current Uri value.
4. Returns a specific status code
Some Web sites URI need to immediately return a response with a specific error or redirect code, such as when the page is temporarily or permanently moved. The easiest way is to use the return instruction. For example, a 404 status code that cannot be found is returned:
Location / wrong/url {return 404;}
The first parameter returned is the response code. The second optional parameter can be the redirected URL (codes 301302303 and 307) or return text in the response body. For example:
Location / permanently/moved/url {return 301 http://www.example.com/moved/here;}
Return instructions can be contained in the context of location and server.
5. Rewrite HTTP response
Sometimes you need to override or change the contents of the HTTP response to replace one string with another. You can use the sub_filter directive to define the override to apply. This directive supports variables and substitution chains, making more complex changes possible.
For example, you can change the reference to an absolute link other than the proxy server:
Location / {sub_filter / blog/ / blog-staging/; sub_filter_once off;}
Another example changes the method from http:// to http://, and replaces the local host address to the host name from the request header domain. The sub_filter_once instruction tells NGINX to apply the sub_filter directive continuously in one location (location):
Location / {sub_filter 'href= "http://127.0.0.1:8080/'' href=" http://$host/'; sub_filter 'img src= "http://127.0.0.1:8080/'' img src=" http://$host/'; sub_filter_once on;}
Note that if another sub_filter match occurs, the response portion modified with sub_filter will no longer be replaced.
At this point, I believe you have a deeper understanding of "Nginx how to configure Web server", might as well come to the actual operation of it! Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.