Home>Article>Operation and Maintenance> Nginx virtual host configuration to achieve access to different domain names
Nginx virtual host configuration to achieve access to different domain names
Introduction:
Nginx is a powerful open source web server software that can be used to configure and manage virtual hosts. Virtual hosting allows multiple domain names to share resources on the same server, and each domain name can have its own independent configuration and functions. This article will introduce how to achieve access to different domain names through Nginx virtual host configuration.
Step 1: Edit the Nginx configuration file
Open the Nginx configuration file, which is usually located at/etc/nginx/nginx.conf
or/etc/nginx/conf.d
Under contents. As needed, you can edit it directly in the main configuration file or create a new domain name configuration file.
Step 2: Configure server
In the configuration file, each virtual host is defined using aserver
block. The following is a basic configuration example:
server { listen 80; server_name example.com; root /var/www/example; index index.html; location / { try_files $uri $uri/ =404; } }
In the above example, thelisten
directive specifies the port number for Nginx to listen to, theserver_name
directive specifies the domain name, androot The
directive specifies the root directory of website files, and theindex
directive specifies the default index file.
Step 3: Configure reverse proxy
If you want to implement the reverse proxy function of the domain name, you can use theproxy_pass
command. The following is a configuration example:
server { listen 80; server_name app.example.com; location / { proxy_pass http://127.0.0.1:8000/; proxy_set_header Host $host; } }
In the above example, theserver_name
directive specifies the domain name, thelocation
block is used to configure the requested URL path, andproxy_pass# The ## directive specifies the target address of the reverse proxy, and the
proxy_set_headerdirective is used to set Header information.
After completing the configuration, save the file and reload the Nginx configuration file to make the modifications effective. You can use the following command to reload the configuration file:
sudo systemctl reload nginxStep 5: Restart the Nginx service
If you need to restart the Nginx service after modifying the configuration file, you can use the following command to restart:
sudo systemctl restart nginxSummary:
Through Nginx’s virtual host configuration, we can easily achieve access to different domain names. Whether serving static content or reverse proxy, Nginx can meet our needs. I hope this article is helpful to you and I wish you a happy use!
The above is the detailed content of Nginx virtual host configuration to achieve access to different domain names. For more information, please follow other related articles on the PHP Chinese website!