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 virtual host with Nginx in LNMP architecture

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)05/31 Report--

This article is about how Nginx configures virtual hosts in LNMP architecture. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.

1. Brief introduction of nginx virtual host

Like the apache service, it also has three different types of virtual hosts: domain name-based virtual hosts, IP-based virtual hosts and port-based virtual hosts. As for the differences, please refer to the previous apache server virtual hosting section.

2. Nginx virtual host configuration environment

System environment

[root@centos6 scripts] # cat / etc/redhat-release

CentOS release 6.5 (Final)

[root@centos6 scripts] # uname-r

2.6.32-431.el6.x86_64

Version of the nginx service

[root@centos6 scripts] # / application/nginx/sbin/nginx-v

Nginx version: nginx/1.10.1

3. Nginx virtual host configuration preparation

The specification of the production environment is very important, which is the focus of operation and maintenance, so create a site directory before configuration.

[root@centos6 ~] # mkdir / www/ {www,bbs,blog}-p

[root@centos6 ~] # tree / www

/ www

+-- bbs

+-- blog

+-- www

3 directories, 0 files

The directory used to store site files

-

Authorization directory

-

[root@centos6] # chown-R nginx.nginx / www

[root@centos6 ~] # ll / www/

Total 12

Drwxr-xr-x. 2 nginx nginx 4096 Dec 4 18:38 bbs

Drwxr-xr-x. 2 nginx nginx 4096 Dec 4 18:38 blog

Drwxr-xr-x. 2 nginx nginx 4096 Dec 4 18:38 www

Next, write something into it and use it for later tests.

[root@centos6 ~] # echo "welcont to mingongge's web stie" > / www/www/index.html

[root@centos6 ~] # echo "welcont to mingongge's bbs stie" > / www/bbs/index.html

[root@centos6 ~] # echo "welcont to mingongge's blog stie" > / www/blog/index.html

[root@centos6 ~] # cat / www/www/index.html

Welcont to mingongge's web stie

[root@centos6 ~] # cat / www/bbs/index.html

Welcont to mingongge's bbs stie

[root@centos6 ~] # cat / www/blog/index.html

Welcont to mingongge's blog stie

Production environment inspection is also very important, inspection, inspection, important things said three times!

4. Nginx virtual host configuration

There are two ways to configure the nginx virtual host. One can configure the configuration file of a virtual host separately as in the previous apache service, and the other can add the server tag to the main configuration file nginx.conf. Next, the first way is to configure the virtual host's configuration file directory and configuration file separately by adding an inclusion relationship in the configuration file, so that it is more common in the actual production environment with more services. It's also easy to maintain.

-

Configure the master profile

-

Just add the following configuration to the last line of the main configuration file nginx.conf

Include extra/vhosts/*.conf

-

Create a virtual host configuration directory

-

[root@centos6 conf] # pwd

/ application/nginx/conf

[root@centos6 conf] # mkdir-p extra/vhosts

-

Create a virtual host profile

-

[root@centos6 conf] # cd extra/vhosts/

[root@centos6 vhosts] # mkdir bbs www blog

[root@centos6 vhosts] # cp.. /.. / nginx.conf www/www.conf

[root@centos6 vhosts] # cp.. /.. / nginx.conf bbs/bbs.conf

[root@centos6 vhosts] # cp.. /.. / nginx.conf blog/blog.conf

Through the main configuration file to modify, in fact, only need the contents of the server tag inside, you can also make a template file, through the cp command to rename, and then modify its content, the effect is the same

-

Configure virtual host profile

-

WWW site virtual host profile (relatively simple)

Server {

Listen 80

Server_name www.mingongge.com

Location / {

Root / www/www

Index index.html index.htm

}

}

It's the same, you just need to change the site directory path and domain name information.

BBS site virtual host profile

Server {

Listen 80

Server_name bbs.mingongge.com

Location / {

Root / www/bbs

Index index.html index.htm

}

}

BLOG site virtual host profile

Server {

Listen 80

Server_name blog.mingongge.com

Location / {

Root / www/blog

Index index.html index.htm

}

}

5. Restart the service and test access

[root@centos6 vhosts] # / application/nginx/sbin/nginx-t

Nginx: the configuration file / application/nginx-1.10.1/conf/nginx.conf syntax is ok

Nginx: configuration file / application/nginx-1.10.1/conf/nginx.conf test is successful

[root@centos6 vhosts] # / application/nginx/sbin/nginx-s reload

[root@centos6 vhosts] # lsof-I: 80

COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME

Nginx 2469 root 6u IPv4 15462 0t0 TCP *: http (LISTEN)

Nginx 2519 nginx 6u IPv4 15462 0t0 TCP *: http (LISTEN)

[root@centos6 vhosts] # ps-ef | grep nginx

Root 2469 1 0 18:47? 00:00:00 nginx: master process / application/nginx/sbin/nginx

Nginx 2519 2469 0 19:14? 00:00:00 nginx: worker process

Open the browser to test the access.

Don't forget the local DNS resolution, otherwise you can't access it through the domain name.

Thank you for reading! This is the end of the article on "how to configure virtual hosts in LNMP architecture". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, you can share it for more people to see!

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

Servers

Wechat

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

12
Report