How to configure multiple sites on Nginx
Sometimes you want to run different sites for different domain names on one server. For example, www.siteA.com serves as a blog and www.siteB.com serves as a forum. You can resolve the IPs of both domain names to your server, but you cannot run two different websites at the same time in the root directory of Nginx. At this time, you need to use a virtual directory. Suppose you put your blog under "/home/user/www/blog" and your forum under "/home/user/www/forum". Now we start the configuration:
Create a "vhost" directory in the Nginx configuration directory. This example assumes that Nginx is installed by default and the configuration directory is in "/etc/nginx"
$ sudo mkdir /etc/nginx/vhost
Create the configuration file of siteA
$ sudo vi /etc/nginx/vhost/vhost_siteA.conf
Enter the following configuration information
server { listen 80; # 监听端口 server_name www.siteA.com siteA.com; # 站点域名 root /home/user/www/blog; # 站点根目录 index index.html index.htm index.php; # 默认导航页 location / { # WordPress固定链接URL重写 if (!-e $request_filename) { rewrite (.*) /index.php; } } # PHP配置 location ~ \.php$ { fastcgi_pass unix:/var/run/php5-fpm.sock; fastcgi_index index.php; include fastcgi_params; } }
Create the configuration file of siteB in the same way as siteA. The only difference between the two is Is the "server_name" and "root" directories
$ sudo vi /etc/nginx/vhost/vhost_siteB.conf
server { ... server_name www.siteB.com siteB.com; # 站点域名 root /home/user/www/forum; # 站点根目录 ... }
Open the nginx.conf file
sudo vi /etc/nginx/nginx.conf
Add the configuration file of the virtual directory to the end of the "http {}" section
http {
...
include /etc/nginx/vhost/*.conf;
}
Restart Nginx service
$ sudo service nginx restart
Now Visit www.siteA.com and www.siteB.com, you will find that the browser will open different sites
Tips for prohibiting access
If your Nginx root directory is set in "/home /user/www", you want to prevent others from accessing your site through "http://IP address/blog" or "http://IP address/forum", the simplest way is to ban IP address access. The method is as follows:
Open the Nginx website default configuration file, remember to back it up first
$ sudo cp /etc/nginx/sites-available/default /etc/nginx/sites-available/default_bak
$ sudo vi /etc/nginx/sites-available/default
Delete all content, leaving only the following configuration
server { listen 80 default_server; server_name _; return 404; }
After restarting Nginx, others will not be able to access through the IP address Website
If you don’t want to ban IP addresses from accessing the entire directory, you just want to prevent others from accessing your blog and forum via IP. Then you need to prohibit directory access to "/blog" and "/forum".
Open the Nginx website default configuration file, the same as above, remember to back it up first
Add the following configuration in the "server { }" section
location ^~ /blog/ { deny all; } location ^~ /forum/ { deny all; }
Restart Nginx

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











Nginx error page configuration, beautify website fault prompts. During the operation of the website, it is inevitable to encounter server errors or other faults. These problems will cause users to be unable to access the website normally. In order to improve user experience and website image, we can configure Nginx error pages to beautify website failure prompts. This article will introduce how to customize the error page through Nginx's error page configuration function, and provide code examples as a reference. 1. Modify the Nginx configuration file. First, we need to open the Nginx configuration.

How to implement Nginx's cross-domain resource sharing (CORS) configuration requires specific code examples. With the popularity of front-end and back-end separation development, cross-domain resource sharing (CORS) issues have become a common challenge. In web development, due to the browser's same-origin policy restrictions, client-side JavaScript code can only request resources with the same domain name, protocol, and port as the page where it is located. However, in actual development, we often need to request resources from different domain names or different subdomains. At this time, you need to use CO

Nginx access control configuration to restrict access to specified users. In a web server, access control is an important security measure used to restrict access rights to specific users or IP addresses. As a high-performance web server, Nginx also provides powerful access control functions. This article will introduce how to use Nginx configuration to limit the access permissions of specified users, and provide code examples for reference. First, we need to prepare a basic Nginx configuration file. Assume we already have a website with a configuration file path of

PHP is a very popular programming language, especially suitable for web development. As a PHP developer, when dealing with some configuration files, you often need to use arrays for management. In this article, we will explore how to use PHP arrays like Nginx configuration files for configuration management. Nginx's configuration file is a very common configuration method that can be edited using text and is very readable. The Nginx configuration file uses a method similar to a PHP array to represent configuration information.

The advanced configuration of Nginx can be implemented through server blocks and reverse proxy: 1. Server blocks allow multiple websites to be run in one instance, each block is configured independently. 2. The reverse proxy forwards the request to the backend server to realize load balancing and cache acceleration.

nginx configuration is the main configuration file, virtual host configuration, HTTP request processing, reverse proxy, load balancing, static file processing, HTTP compression, SSL/TLS support, virtual host configuration and log files.

How to use NGINX and PM2 to configure a VPS server. In the process of building a web server, using NGINX and PM2 is a common configuration method. NGINX is a high-performance web server commonly used for reverse proxy and load balancing. PM2 is a process management tool that can run and manage Node.js applications on the server. This article will introduce how to configure a VPS server using NGINX and PM2, and provide specific code examples. Step 1: Install NGINX and PM2

How Nginx implements access control configuration based on the domain name of the request source requires specific code examples. Nginx is a high-performance web server software. It can not only serve as a static file server, but can also implement flexible access control through configuration. This article will introduce how to implement access control configuration based on the request source domain name through Nginx, and provide specific code examples. The Nginx configuration file is usually located in /etc/nginx/nginx.conf. We can add relevant configurations to this file.