Nginx部署多個站點

Nginx部署多個站點

一,介紹與需求

1.1,介紹

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

1.2,需求

有時候想在一臺服務器上爲不一樣的域名/不一樣的二級域名運行不一樣的站點。好比www.webA.com做爲官網前臺,www.webB.com/admin.webA.com做爲後臺管理系統。能夠把你的服務器IP分別解析到兩個域名上,而後反向代理不一樣的站點,站點的服務名必須與域名對應。html

二,Nginx配置

第一步:新建webServer文件夾nginx

1 mkdir /usr/local/nginx/webServer

第二步:進入webServer目錄web

1 cd webServer

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

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配置文件spa

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主配置文件code

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

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

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便可。

相關文章
相關標籤/搜索