In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)05/31 Report--
This article introduces the relevant knowledge of "Nginx initialization configuration method". In the actual case operation process, many people will encounter such difficulties. Next, let Xiaobian lead you to learn how to deal with these situations! I hope you can read carefully and learn something!
basic concepts
The most common use of nginx is to provide reverse proxy services, so what reverse proxy? The positive agent believes that many mainland compatriots have used it on this magical land. The principle is roughly as follows:
The proxy server acts as an intermediary on the client side to accept requests, hide the real client, and obtain resources from the server. If the proxy server is outside the Great Wall, it can also help us achieve the purpose of crossing the Great Wall. The reverse proxy, as the name implies, is the reverse proxy server as the intermediary of the server, hiding the server that actually provides the service. The principle is roughly as follows:
Of course, this is not to achieve the Great Wall, but to achieve a series of functions such as safety and Load Balancer. The so-called security means that the client's request will not fall directly on the server of the intranet, but will be forwarded through a layer of proxies. At this layer, a series of policies such as security filtering, flow control, and anti-ddos can be implemented. Load Balancer means that we can horizontally expand the number of servers that really provide services on the backend, and the proxy forwards requests to each server according to rules, so that the load of each server is close to balanced.
Nginx is such a reverse proxy service that is currently popular.
Under ubuntu, you can skip the compilation and installation process and directly apt-get
The copy code is as follows:
sudo apt-get install nginx
After installation, you can directly pass through:
The copy code is as follows:
sudo service nginx start
To start the nginx service, nginx defaults to port 80 forwarding, we can then visit http://locallhost browser to check.
initial configuration
The default configuration file for nginx is located at
The copy code is as follows:
/etc/nginx/nginx.conf
The best way to learn configuration is to start with examples. We don't look at other configurations first, but directly look at the configuration related to the nginx default page. There is a line in the configuration file:
The copy code is as follows:
include /etc/nginx/sites-enabled/*;
This line loads an external configuration file. There is only one default file under the sites-enabled folder. This external configuration file is the default proxy responsible for our nginx. After shrinking the contents of the configuration, you get the following lines:
The copy code is as follows:
server {
server_name localhost;
listen 80 default_server;
listen [::]:80 default_server ipv6only=on;
root /usr/share/nginx/html;
index index.html index.htm;
location / {
try_files $uri $uri/ =404;
}
}
A large website usually has many subordinate sites, with their own servers providing corresponding services. In nginx, we can isolate these different service configurations through a concept called virtual host, which is the meaning of server in the above configuration. For example, Google has translation and academic products. We can configure two servers in the nginx configuration file. The server names are translate.google.com and scholar.google.com respectively. In this way, different URL requests will correspond to the corresponding settings of nginx and be forwarded to different backend servers. The servername here matches the host line in the client http request.
In this case server_name is localhost, which is why we can access the page configuration in the browser via localhost. The following two listens correspond to ipv4 and ipv6 listening ports respectively. If it is set to 8080, then we can only access the default page through localhost: 8080.
Default_server means that if there are other http request host does not exist in nginx settings, then use this server configuration to handle. For example, if we visit 127.0.0.1, it will also fall to this server to handle it.
Each URL request corresponds to a service, which nginx processes and forwards, either a local file path or a service path from another server. The path matching is done by location. We can think of server as a configuration corresponding to a domain name, while location is a configuration of a finer path under a domain name.
All requests where location matches/start, i.e./xxx or/yyy under localhost, have to go through the following configuration. In addition to this simple and rough matching, nginx also supports regular and exact equality and other fine matching methods. Tryfiles means nginx will access files in the next order, returning the first match. For example, if you request localhost/test, it will look for/test file, and if it cannot be found, it will return a 404. In addition, we can also implement reverse proxy and Load Balancer in the configuration of location with proximass, but this simplest configuration does not involve
root refers to a local folder as the root path of all url requests. For example, if the user requests localhost/test, nginx will go to the test file in the/usr/share/nginx/html folder and return it.
Index is the default access page. When we visit localhost, it will automatically look for index.html and index.htm under the root file path in order and return the first result found.
location Advanced Configuration
The above configuration only maps the user's url to a local file, and does not implement the legendary reverse proxy and Load Balancer (of course, nginx does static file distribution is also thought of), let's further configure location to see how to achieve.
It's easy to configure, for example, if I want to redirect all requests to port 8080 on a machine that actually provides the service, as long as:
The copy code is as follows:
location / {
proxy_pass 123.34.56.67:8080;
}
All requests are then back-proxied to www.example.com. 123.34.56.67 In this way we reverse proxy function is achieved, but can proxy to a server where there is any Load Balancer ah? This is where the upstream module of nginx comes in.
The copy code is as follows:
upstream backend {
ip_hash;
server backend1.example.com;
server backend2.example.com;
server backend3.example.com;
server backend4.example.com;
}
location / {
proxy_pass http://backend;
}
We specify a group of machines in the upstream and name this group backend, so that we implement reverse proxy plus Load Balancer on four machines as long as the request is transferred to backend in the upstream in proxy. The iphash indicates that we are balancing the allocation according to the ip address of the user.
For the configuration to take effect, we don't have to restart nginx just reload the configuration.
The copy code is as follows:
sudo service nginx reload
"Nginx initialization configuration method" content is introduced here, thank you for reading. If you want to know more about industry-related knowledge, you can pay attention to the website. Xiaobian will output more high-quality practical articles for everyone!
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.