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 a virtual host for nginx to access multiple websites on one server

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

Share

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

Editor to share with you how to configure nginx virtual host to achieve access to multiple websites on a server, I believe most people do not know much about it, so share this article for your reference, I hope you can learn a lot after reading this article, let's go to know it!

On one server, visit different websites

There are usually two ways to distinguish:

1. Port number that is monitored

two。 Through the domain name

1. Access different hosts through ports:

Configuration file for Nginx:

/ usr/local/nginx/conf/nginx.conf

Centos file default encoding format latin1

The command to view the encoding format:: set fileencoding

# user nobody;worker_processes 1 Chinese errorists log logs/error.log;#error_log logs/error.log notice;#error_log logs/error.log info;#pid logs/nginx.pid;events {worker_connections 1024;} # # A http node http {include mime.types; default_type application/octet-stream # log_format main'$remote_addr-$remote_user [$time_local] "$request" #'$status $body_bytes_sent "$http_referer" #'"$http_user_agent"$http_x_forwarded_for"; # access_log logs/access.log main; sendfile on; # tcp_nopush on; # keepalive_timeout 0; keepalive_timeout 65; # gzip on # server node, that is, the configuration you need to access the website # A server node is the port number of a virtual host server {listen 80; # listening. The default for accessing the website is port 80 server_name localhost; # that is, the domain name # charset koi8-r; # access_log logs/host.access.log main; location / {# location root html # locate the html folder under the nginx root directory index index.html index.htm; # set the home page of the website}

Multiple server can be configured at this time, that is, different hosts are configured.

Add virtual host: (distinguished by port number)

Server {listen 81; server_name localhost; # charset koi8-r; # access_log logs/host.access.log main; location / {html81 folder index index.html index.htm; ``} newly created under the root directory of root html-81; # nginx

After editing the file, we reload the configuration file

Through the command:. / nginx-s reload

Effect:

We know that when multiple websites are configured on a server, it is impossible to distinguish them by port number, so next I need to distinguish them by domain name.

two。 Distinguish different virtual hosts by domain name

What is a domain name?

The domain name is the URL.

For example: www.baidu.com

Usually when we access the domain name, we need to resolve the domain name through the dns server

Dns server: resolves domain names to ip addresses. What is saved is the mapping between the domain name and ip.

A domain name corresponds to an ip address, and an ip address can be bound by multiple domain names.

Local tests can modify the hosts file.

Modify the hosts file of window: (C:\ Windows\ System32\ drivers\ etc)

You can configure the mapping relationship between domain name and ip. If the corresponding relationship between domain name and ip is configured in the hosts file, you do not need to go to the dns server!

Continue to configure under the nginx.conf file you just had:

Server {listen 80; server_name www.taobao.com; # charset koi8-r; # access_log logs/host.access.log main; location / {root html-taobao; index index.html index.htm;}} server {listen 80; server_name www.baidu.com; # charset koi8-r; # access_log logs/host.access.log main; location / {root html-baidu Index index.html index.htm;}

Configuration of domain name:

192.168.25.148 www.test.com

192.168.25.148 www.yiyou.com

Restart the nginx service

Observe the effect:

These are all the contents of the article "how to configure a virtual host for nginx to access multiple websites on one server". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to 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

Servers

Wechat

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

12
Report