Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to configure Nginx virtual host

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

In this article, the editor introduces "how to configure the Nginx virtual host" in detail, the content is detailed, the steps are clear, and the details are handled properly. I hope this article "how to configure the Nginx virtual host" can help you solve your doubts.

I. configuration of Nginx virtual host

Virtual hosts: in general, in order to make each server available to more users, a server can be divided into many virtual sub-servers, each of which is independent of each other. These servers are separated according to virtualization technology, so that one server can be virtualized into many sub-servers. We call sub-servers virtual hosts. After we have built the Nginx server, there is only one Nginx server at this time. If we configure this server as a virtual host, we can split a Nginx server into multiple independent sub-servers.

There are two main steps to configure a virtual host in Nginx:

1. Create a virtual host IP

View your own host ip through ifconfig, and then create a virtual host Ip based on the host ip.

Command: ifconfig eth2:2 121.42.41.145 broadcast 121.42.43.255 netmask 255.255.252.0

After execution, as shown in the figure:

2. Bind IP address and virtual host.

Nginx.conf: this file is the system configuration file for nginx, and it is not recommended to change it. We generally use a custom file, and then load the file to achieve the same effect.

Create a configuration file to create a xnzj.conf in the / usr/local/nginx/conf directory.

# = number of work-derived processes (it is recommended to set the same or twice as many cores as cpu) = worker_processes 1 leading = setting the maximum number of connections = events {worker_connections 1024;} # = information about http protocol = http {server {# = IP address and port of the virtual host to be monitored = listen 121.42.41.144l80; # = name of the virtual host = server_name 121.42.41.144 # = the log file of the virtual host server = access_log logs/server144.access.log combined; # = default request resource = location / {root html/server144; # = nginx will look for index.html first. If you can't find it, find index.htm index index.html index.htm. }} server {# = IP address and port to listen on the virtual host = listen 121.42.41.145 listen 80; # = the name of the virtual host = server_name 121.42.41.145; # = the log file of the virtual host server = access_log logs/server145.access.log combined # = default request resource = location / {root html/server145; index index.html index.htm;}

Create the corresponding virtual host default resource under / usr/local/nginx/html

/ usr/local/nginx/html/server144/index.html; / usr/local/nginx/html/server145/index.html

Have Nginx load our custom configuration file (my configuration file: xnzj.conf)

Execute the command: / usr/local/nginx/sbin/nginx-c / usr/local/nginx/conf/xnzj.conf

II. Configuration of logs

When the Nginx server is running, there will be a variety of operations, and these key operation information will be recorded in files, which are called log files. The record of the log file has a format, we can record it according to the default format of the system, or we can record it according to our custom format. We can use the log_format directive to set the recording format of the log file for the Nginx server.

Configuration: open the nginx.conf file and open the lower code of the comments.

# combined: log output format # remote_addr client request address # remote_user: client user name # request: requested address (server resource location) # status: user request status # body_bytes_sent: resource size of server response (number of bytes) # http_referer: source page # http_user_agent: client browser information # http_x_forwarded_for: client Ip address log_format combined'$remote_addr-$remote_user [$time_local] "$request"'$status $body_bytes_sent "$http_referer"'"$http_user_ Agent "$http_x_forwarded_for"' # = log file access_log:off; means to turn off log = access_log logs/access.log combined

Log cutting:

In order to make the log file storage of Nginx more reasonable and orderly, we need to store the log file separately, for example, we can separate the log file by time, today's log file is stored in one file, tomorrow's log file is stored in another new file, and so on. At this time, we will use the log file cutting operation.

Log slicing steps:

1. Create a batch file

Execute [root@iZ28b4kreuaZ logs] # touch cutlog.sh under the / usr/local/nginx/logs directory

2. Add content to the file:

Dating $(date +% Y%m%d) mv / usr/local/nginx/logs/access.log ${D} .logkill-USR1 $(cat / usr/local/nginx/nginx.pid)

3. Execute batch files regularly. Execute the crontab-e command to add the following

23 59 * / bin/bash / usr/local/nginx/logs/cutlog.sh

III. Nginx cache configuration

When we browse a web page in a browser, we will store some information on that page (such as the pictures on this page) locally, and when we visit the page for the second time, some information on this page can be loaded locally, which will be much faster. The information stored locally is called caching. However, when there is too much cache, the cache files will be very large, affecting our normal online activities. Therefore, the cache needs to be cleaned regularly.

Configuration method: add the following code under location in http {server {}} of / usr/local/nginx/conf/nginx.conf configuration file:

# = cache configuration = location ~. *\. (jpg | png | swf | gif) ${expires 2dscape # clear} location ~. *\. (css | js)? ${expires:1h;#} 4. Gzip compression configuration of Nginx

The compression function we are talking about here refers to gzip compression technology. Through gzip compression technology, the content size of the original web page can be reduced to 30% of the original. In this way, when users visit the web page, because the content transmitted is much smaller than the original content, the access speed will be much faster. The Nginx server supports gzip compression technology, but it needs to be configured.

Configuration method: add the following code to the http {} of the / usr/local/nginx/conf/nginx.conf configuration file:

Gzip on;# enable compression gzip_min_lenth 1kbot # set the smallest unit of compression gzip_buffers 4 16kbot # create the compressed file cache size gzip_http_version 1.1ash # the protocol and its version gzip_vary:on;# open to determine whether the client browser supports compression technology 5. Nginx automatic column directory configuration

When the client accesses a folder on the server through a browser, if there is a default home file on the folder, such as index.html, then the user will automatically access the index.html web page. However, when there is no default home page file such as index.html, assuming that there are other files in this folder, users will not be able to access the contents of our folder without configuring the function of automatically listing directories. But when we have configured the automatic column directory feature, we can see a list of all the files under the folder, and the list directory is automatically listed.

Two conditions are required to implement an automatic column directory:

1. There is no default home page file such as index under the accessed folder.

two。 The server is configured with the automatic column directory feature.

Configuration method: add the following code to the http {server {}} of the / usr/local/nginx/conf/nginx.conf configuration file:

Location / {root html; index index.html index.htm; autoindex on # enable automatic listing directory} so far, the article "how to configure Nginx virtual hosts" has been introduced. If you want to master the knowledge points of this article, you still need to practice and use it. If you want to learn more about related articles, please follow the industry information channel.

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.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report