![](https://static.wixstatic.com/media/68165d_917aba7f7ae84e84809430448a5b1e77~mv2.png/v1/fill/w_750,h_375,al_c,q_85,enc_auto/68165d_917aba7f7ae84e84809430448a5b1e77~mv2.png)
Nginx là một máy máy chủ web được sử dụng như một proxy ngược, cân bằng tải và HTTP Cache.
Tương tự như virtual host trên Apache cho phép chạy nhiều trang web trên một server. Server blocks trong Nginx cho phép một server lưu trữ nhiều tên miền bằng cách matching system.
Trong hướng dẫn này mình sẽ cài đặt Nginx server blocks cho hai websites nguyento.local và tuannguyen.local.
1. Cài Nginx
Trước tiên server của bạn phải cài Nginx nhấn vào đây để xem hướng dẫn cài Nginx.
2. Tạo tên miền cho website
Ở đây mình sẽ tạo hai tên miền để test local.
# vi /etc/hosts
Thêm hai dòng sau:
192.168.100.75 nguyento.local
192.168.100.75 tuannguyen.local
IP server mình là 192.168.100.75.
Ping thử tên miền đã chạy được chưa nhé.
![](https://static.wixstatic.com/media/68165d_ce4b9230d9c64acdbb982b02d238eb31~mv2.png/v1/fill/w_642,h_116,al_c,q_85,enc_auto/68165d_ce4b9230d9c64acdbb982b02d238eb31~mv2.png)
![](https://static.wixstatic.com/media/68165d_26b1ef47b93741158bdcf5e1e9aa07f2~mv2.png/v1/fill/w_642,h_115,al_c,q_85,enc_auto/68165d_26b1ef47b93741158bdcf5e1e9aa07f2~mv2.png)
3. Tạo nơi lưu website
Tạo nơi lưu website thứ nhất.
# mkdir /usr/share/nginx/nguyento.local
Tạo nơi lưu website thứ hai.
# mkdir /usr/share/nginx/tuannguyen.local
4. Tạo website
Tạo page cho website thứ nhất.
# vi /usr/share/nginx/nguyento.local/index.html
<html>
<head>
<title>Nguyento.local</title>
</head>
<body>
<p>Welcome to Nguyento.local</p>
</body>
</html>
Tạo page cho website thứ hai.
# vi /user/share/nginx/tuannguyen.local/index.html
<html>
<head>
<title>Tuannguyen.local</title>
</head>
<body>
<p>Welcome to Tuannguyen.local</p>
</body>
</html>
5. Tạo server block files
Tạo server block file cho website thứ nhất.
# vi /etc/nginx/conf.d/nguyento.local.conf
# Virtual host nguyento.local
server {
listen 80;
server_name nguyento.local; # Tên miền website thứ nhất
root /usr/share/nginx/nguyento.local;
index index.html index.htm;
}
Tạo server block file cho website thứ hai.
# vi /etc/nginx/conf.d/tuannguyen.local.conf
# Virtual host tuannguyen.local
server {
listen 80;
server_name tuannguyen.local; # Tên miền website thứ 2
root /usr/share/nginx/tuannguyen.local; # Nơi lưu website thứ 2
index index.html index.htm;
}
Khởi động lại Nginx
# systemctl restart nginx
Kiểm tra kết quả nhé.
![](https://static.wixstatic.com/media/68165d_aaf11e0809ef44d7ba6cdf2afce72191~mv2.png/v1/fill/w_799,h_254,al_c,q_85,enc_auto/68165d_aaf11e0809ef44d7ba6cdf2afce72191~mv2.png)
![](https://static.wixstatic.com/media/68165d_346681c7571d49ddba309f6825ec521f~mv2.png/v1/fill/w_802,h_236,al_c,q_85,enc_auto/68165d_346681c7571d49ddba309f6825ec521f~mv2.png)
Vậy là chúng ta đã cấu hình xong Nginx server block rồi nhé.
À trong hướng dẫn này mình tạo trang web bằng code html nếu code PHP thì nhấn vào đây để xem hướng dẫn nhé !
Commenti