Nginx部署多個站點


Nginx部署多個站點

一,介紹與需求

1.1,介紹

詳細介紹請看nginx代理部署Vue與React項目,在這兒主要介紹多個站點的配置

1.2,需求

有時候想在一台服務器上為不同的域名/不同的二級域名運行不同的站點。比如www.webA.com作為官網前台,www.webB.com/admin.webA.com作為后台管理系統。可以把你的服務器IP分別解析到兩個域名上,然后反向代理不同的站點,站點的服務名必須與域名對應。

二,Nginx配置

第一步:新建webServer文件夾

1 mkdir /usr/local/nginx/webServer

第二步:進入webServer目錄

1 cd webServer

第三步:新建站點A配置文件

1 vim webA.conf

在站點A中寫入如下配置信息:

1 server {
2     listen       80;
3     server_name  www.webA.com;
4 
5     location / {
6         root   html1;
7         index  index.php index.html index.htm;
8     }
9 }

第四步:新建站點B配置文件

1 vim admin.conf

在站點B中寫入如下配置信息:

 1 upstream demostream {
 2   server 127.0.0.1:9090  weight = 4;
 3 }
 4 
 5 server {
 6   listen       80;
 7   server_name  admin.webA.com,www.webB.com;
 8 
 9   #charset koi8 - r;
10   #access_log  logs / host.access.log  main;
11 
12   location / {
13      proxy_pass  http://demostream/dist/;
14      proxy_set_header Host $host;
15      proxy_set_header  X- Real - IP        $remote_addr;
16      proxy_set_header  X - Forwarded - For  $proxy_add_x_forwarded_for;
17      proxy_set_header X - NginX - Proxy true;
18      proxy_set_header Connection "upgrade";
19      proxy_set_header Upgrade $http_upgrade;
20  }
21 }

第五步:配置nginx主配置文件

1 vim  /usr/local/nginx/conf/nginx.conf

在nginx配置文件http塊中,加入下面一句

1  include /usr/local/nginx/webServer/*.conf; #表示包含我們剛才建立的配置文件

第六步:檢查nginx配置文件是否正確

1 ./nginx -t

第七步:重啟nginx

1 ./nginx -s reload

第八步:nginx只允許域名訪問,禁止ip訪問

新加的server(注意是新增,並不是在原有的server基礎上修改)

server {
  listen 80 default;
  server_name _;
  return 403;
}

第九步:配置域名

站點的服務名必須與域名對應,即server_name就是相應的二級域名;同時需要在hosts文件中添加對應的配置

1 vim /etc/hosts

添加如下配置信息:

1 127.0.0.1 www.webA.com
2 127.0.0.1 admin.webA.com
3 127.0.0.1 www.webB.com

使用hostname+定義的主機名是hosts文件生效

1 hostname testHost

輸入hostname可查看定義的主機名。

綁定域名解析,添加記錄->綁定服務器的公網IP即可,如下所示,記錄值輸入公網IP即可。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM