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

The method of nginx accessing the same address in the practice of Nginx Virtual Host configuration (2)

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

Share

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

The method of nginx accessing the same address in the practice of Nginx Virtual Host configuration (2)

I. introduction of virtual host aliases

The virtual host alias is to set one or more domain names for the virtual host in addition to the main domain name, so as to realize the function that multiple domain names visited by users correspond to the same virtual host website. In the production environment, take the virtual host of the www.afeilinux.com domain name as an example, when you add an alias afeilinux.com to it, the website content that appears in the domain name is the same as the result of visiting www.afeilinux.com.

II. Implementation methods

The first method: alias configuration of Nginx virtual host

Change wtf.conf profile

[root@nginx-oldboy conf] # vim extra/wtf.conf

3 server_name www.afeilinux.com afeilinux.com

Compare configuration files before and after Chan

Syntax checking and reloading

[root@nginx-oldboy conf] # nginx- t

[root@nginx-oldboy conf] # nginx- s reload

Change hosts

[root@nginx-oldboy conf] # vim / etc/hosts

Add: 192.168.8.117 www.afeilinux.com afeilinux.com

test

[root@nginx-oldboy conf] # curl www.afeilinux.com

First test

[root@nginx-oldboy conf] # curl afeilinux.com

First test

Practical application of virtual host alias in production

When most corporate websites want to visit www.afeilinux.com and afeilinux.com, they browse the same page. If there is such a need, you can have afeilinux.com appear as an alias, and then both domain names should be resolved to the IP address of the server.

The second method: Nginx rewrite

Nginx rewrite concept

Like Web service software such as Apache, the main function of Nginx rewrite is to implement URL address rewriting. Nginx's rewrite rules need the support of PCRE software, that is, rule matching is carried out through perl compatible regular expression syntax. So when installing Nginx, we should install PCRE software, and at the same time, let Nginx support the function of rewrite. When compiling the default parameters, Nginx will install the module that supports rewrite. However, it must be supported by PCRE software.

View information about installing Nginx versions and modules:

[root@nginx-oldboy ~] # nginx- V

Rewrite instruction syntax

Syntax: rewrite regex replacement [flag]

Default value: none

Application location: server, location, if

Rewrite is the key instruction to implement URL rewriting. According to the contents of the regex (regular expression) section, it is redirected to the replacement section, ending with the flag tag. Here is a simple example of a URL rewrite jump:

Rewrite ^ / (.*) http://www.afeilinux.com/$1 permanent

In the above instruction, rewrite is a fixed keyword, which means that a rewrite matching rule is enabled, and the regex part is ^ / (. *). This is a regular expression that matches all, and jumps to http://www.afeilinux.com/$1 after a successful match. The $1 here is taken from the parentheses in the previous regex section, and ends with a permanent; that is a permanent 301 redirect flag, that is, to jump to the following http://www.afeilinux.com address.

Enterprise Application scenario of Nginx rewrite

(1) the URL browsed by users can be adjusted to look more standardized and meet the needs of developers and product personnel.

(2) in order to allow search engines to include website content and make the user experience better, the company will disguise the Dingtai URL address as a static address to provide services.

(3) after the website changes to a new domain name, let the access of the old domain name be transferred to the new domain name, for example, change JD.com 's 360buy to jd.com.

(4) URL jump according to the information of special variables, directories and clients.

Nginx rewrite 301Jump

Background: jump domain name from afeilinux.org to www.afeilinux.org

View the configuration of the original virtual host:

[root@nginx-oldboy extra] # cat org.conf

Virtual host configuration after change:

[root@nginx-oldboy extra] # cat org.conf

Note: when users access afeilinux.org and any of the following content, they will jump to the address corresponding to www.afeilinux.org through this rewrite.

Syntax checking and nginx restart

[root@nginx-oldboy extra] # nginx- t

[root@nginx-oldboy extra] # nginx- s reload

test

Description: the address before access is afeilinux.org, and the address after access is www.afeilinux.org.

Curl Test:

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