In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-21 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/01 Report--
In this issue, the editor will bring you about how to configure multiple domain names in the nginx server. The article is rich in content and analyzed and described from a professional point of view. I hope you can get something after reading this article.
Let me talk about the use of server_name:
Matching order of server_name
The server_name directive in Nginx is mainly used to configure a name-based virtual host. The matching order of the server_name instruction after receiving the request is as follows:
1. Accurate server_name matching, for example:
Server {listen 80th serverswimname ssdr.info www.ssdr.info;...}
2. A string that begins with a * wildcard:
Server {listen 80th serverroomname * .ssdr.info;...}
3. A string that ends with a * wildcard:
Server {listen 80th serverswimname www.*;...}
4. Match the regular expression:
Server {listen 80th serverroomname ~ ^ (?. +)\ .howtocn\. Org $;.}
Nginx will match the server name in the order of one match, and only one match will stop the search, so we must distinguish the matching order when using this instruction (similar to the location instruction).
One of the practical features of the server_name directive is that you can use the capture function of regular expressions to simplify the configuration file as much as possible. after all, it is not convenient for the daily maintenance of configuration files that are too long. Here are two specific applications:
Configure multiple sites in a server block:
Server {listen 80th serverroomname ~ ^ (www\.)? (. +) $; index index.php index.html;root / data/wwwsite/$2;}
The home directory of the site should look like this:
/ data/wwwsite/ssdr.info/data/wwwsite/linuxtone.org/data/wwwsite/baidu.com/data/wwwsite/google.com
This allows you to use only one server block to complete the configuration of multiple sites.
Configure multiple secondary domain names for a site in a server block.
In the actual website directory structure, we usually create a separate directory for the secondary domain name of the site. Similarly, we can use regular capture to configure multiple secondary domain names in a server block:
Server {listen 80th serverroomname ~ ^ (. +)?\ .howtocn\ .org $; index index.html;if ($host = ssdr.info) {rewrite ^ http://www.ssdr.info permanent;} root / data/wwwsite/ssdr.info/$1/;}
The directory structure of the site should be as follows:
/ data/wwwsite/ssdr.info/www//data/wwwsite/ssdr.info/nginx/
So when accessing www.ssdr.info, the root directory is / data/wwwsite/ssdr.info/www/,nginx.ssdr.info, it is / data/wwwsite/ssdr.info/nginx/, and so on.
The function of the later if statement is to redirect the orientation of ssdr.info to www.ssdr.info, which not only solves the home directory access of the website, but also increases the domain name weight of www.ssdr.info in seo.
Multiple regular expressions
If you use regular in server_name, and the following location field uses regular matching, you will not be able to use a reference such as $1. The solution is to assign it to a named variable through the set directive:
Server {listen 80th serverkeeper name ~ ^ (. +)?\ .howtocn\ .org $; set $www_root $1x root / data/wwwsite/ssdr.info/$www_root/;location ~. *\ .php? ${fastcgi_pass 127.0.1VOV 9000th fastcgistered index index.php;fastcgi_param SCRIPT_FILENAME / data/wwwsite/ssdr.info/$fastcgi_script_name;include fastcgi_params;}}
Nginx reverse proxies different domain names to another server proxy_pass and $host
If you want a VPS to be dedicated to the front end of another VPS, each time a domain name is added to the back-end VPS, the front-end VPS needs to add a domain name to reverse proxy at the same time. As the front-end VPS, if you add the domain name of the back-end VPS one by one, this is particularly troublesome. Can it automatically reverse proxy the back-end VPS, using proxy_pass and $host?
The following example is set up with lnmp as the installation environment to save trouble
Modify the nginx.conf file of the frontend VPS to read as follows:
Server {listen 80 per servertimeout $host;location / {proxy_pass http://www.31.gd/;proxy_set_header Host $host;proxy_redirect off;proxy_set_header X-Real-IP $remote_addr;proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;proxy_connect_timeout 60 per proxy read through timeout 600 per proxy endgame timeout 600;}
Let's modify the following together.
Location /. (php | php5)? ${fastcgi_pass unix:/tmp/php-cgi.sock;fastcgi_index index.php;include fcgi.conf;} location / status {stub_status on;access_log off;} location /. (gif | jpg | jpeg | png | bmp | swf) ${expires 30d;} location /. (js | css)? ${expires 12h;}
In this way, the front-end VPS can reverse proxy any domain name to the back-end VPS. As long as the domain name is resolved to the front-end VPS, and the back-end VPS binds the domain name, then it can be accessed directly.
Configuration of a nginx with multiple domain names and multiple tomcat
Multiple domain names, 2 of which need to support pan-domain name resolution:
1 、 www.abc.com
2 、 www.bcd.com
3. * .efg.com
4. * .hij.com
One of them is a tomcat,4, which is an independent tomcat. The front end of a nginx, through the configuration of multiple virtual hosts to achieve this deployment.
Enter the / etc/nginx/conf.d directory, where all virtual host configuration files are stored and configured.
Configure to support pan-domain names
# # A virtual host using mix of IP-, name-, and port-based configuration#server {listen 81 proxy_add_x_forwarded_for; name *. Efg.com;location / {proxy_pass http://localhost:8080;proxy_set_header Host $host;proxy_set_header X-Real-IP $remote_addr;proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;}} # # A virtual host using mix of IP-, name-, and port-based configuration#server {listen 81 A virtual host using mix of IP-, name-, and port-based configuration#server serverSecretname * .hij.com Location / {proxy_pass http://localhost:8081;proxy_set_header Host $host;proxy_set_header X-Real-IP $remote_addr;proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;}}
The key to pan-domain name resolution is the red part. If there is no red part, the tomcat virtual hosts corresponding to backend ports 8080 and 8081 will not be able to obtain domain name information, resulting in the corresponding domain name information cannot be obtained by backend tomcat.
When the backend TOMCAT supports pan-domain name resolution, you need to set host name to localhost to support pan-domain name pointing.
Nginx multi-domain name configuration
Nginx binds multiple domain names and writes multiple domain name rules in a configuration file, or sets up multiple domain name configuration files separately. I usually create a file for each domain name for ease of management, and some similar domain names can also be written in a general configuration file.
I. how to write one file for each domain name
First, open the nginx domain name configuration file to store the directory: / usr/local/nginx/conf/servers. If you want to bind the domain name www.web126.com, create a file: www.web126.com.conf in this directory, and then write the rules in this file, such as:
Server {listen 80th serverSecretname www.web126.com; # bind domain name index index.htm index.html index.php; # default file root / home/www/web126.com; # website root include location.conf; # call other rules, can also remove}
Then restart the nginx server, and the domain name is bound successfully.
The Nginx server restart command: / etc/init.d/nginx restart.
2. How to write multiple domain names in one file
The same is true for adding multiple domain names to a file. As long as you write down the above single domain name repeatedly, you can ok it, such as:
Server {listen 80 serverSecretname www.web126.com; # bind domain name index index.htm index.html index.php; # default file root / home/www/web126.com; # website root directory include location.conf; # call other rules, you can also remove} server {listen 80th serverSecretname msn.web126.com; # bind domain name index index.htm index.html index.php; # default file root / home/www/msn.web126.com # website root directory include location.conf; # call other rules, can also remove}
Domain name without www plus 301Jump
If you want to add 301jump to a domain name without www, it is the same as binding a domain name without www, but instead of writing a website directory, you will perform 301jump instead, such as:
Server {listen 80th serverroomname web126.com;rewrite ^ / (. *) http://www.web126.com/$1 permanent;}
Add 404 web pages
Add 404 web pages, you can add them directly, such as:
Server {listen 80th serverSecretname www.web126.com; # bind domain name index index.htm index.html index.php; # default file root / home/www/web126.com; # website root directory include location.conf; # call other rules, you can also remove error_page 404 / 404.html;}
Finally, there is another method to note. It may be necessary to prohibit IP from directly accessing port 80 or to prohibit non-local domain names from binding our IP. In that case, we should
As follows, put it on the top of the first server:
Server {listen 80 default;server_name _; return 403;} the above is how to configure multiple domain names in the nginx server. If you happen to have similar doubts, please refer to the above analysis to understand. If you want to know more about it, you are 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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.