在一個(gè)服務(wù)器上部署多個(gè)站點(diǎn),需要開放多個(gè)端口來訪問不同的站點(diǎn),流程很簡單,調(diào)試花了2小時(shí),記錄一下:
主域名多端口訪問
在DNS NameServer設(shè)置A記錄
將 www.xxx.com 指向服務(wù)器ip
開放所需端口,修改nginx配置文件
比如我們有兩個(gè)服務(wù)分別開放在80端口和8080端口
如果有iptable,先開放端口:
iptables -A INPUT -ptcp --dport 80 -j ACCEPT
iptables -A INPUT -ptcp --dport 8080 -j ACCEPT
修改配置文件:
#path: /usr/local/nginx/conf/nginx.conf
server {
listen 80;
server_name www.xxx.com;
access_log /data/www/log/33.33.33.33_nginx.log combined;
index index.html index.htm index.php;
include /usr/local/nginx/conf/rewrite/none.conf;
root /data/www/website/33.33.33.33:80;
location ~ [^/]\.php(/|$) {
fastcgi_pass unix:/dev/shm/php-cgi.sock;
fastcgi_index index.php;
include fastcgi.conf;
}
location ~ .*\.(gif|jpg|jpeg|png|bmp|swf|flv|ico)$ {
expires 30d;
access_log off;
}
location ~ .*\.(js|css)?$ {
expires 7d;
access_log off;
}
}
server {
listen 8080;
server_name A.xxx.com;
access_log /data/www/log/33.33.33.33:8080_nginx.log combined;
index index.html index.htm index.php;
include /usr/local/nginx/conf/rewrite/none.conf;
root /data/www/website/33.33.33.33:8080;
location ~ [^/]\.php(/|$) {
fastcgi_pass unix:/dev/shm/php-cgi.sock;
fastcgi_index index.php;
include fastcgi.conf;
}
location ~ .*\.(gif|jpg|jpeg|png|bmp|swf|flv|ico)$ {
expires 30d;
access_log off;
}
location ~ .*\.(js|css)?$ {
expires 7d;
access_log off;
}
}
關(guān)鍵就是兩個(gè) server 段配置,你也可以把這兩段拆成兩個(gè)配置文件,放到
目錄下面;
子域名多端口訪問
這種訪問比較傻,因?yàn)槟愕?080端口的訪問需要 http://xxx.com:8080 這樣的格式;
而且如果有兩個(gè)不同的cgi,比如80端口對應(yīng)一個(gè)php web服務(wù), 8080端口對應(yīng)一個(gè)nodejs web服務(wù);而我們的nodejs自帶web服務(wù),已經(jīng)在8080端口監(jiān)聽了,這怎么辦?
這個(gè)時(shí)候我們需要Nginx的反向代理功能,并在DNS Server上面增加一條A記錄,最終實(shí)現(xiàn)
- www.xxx.com 訪問80端口
- A.xxx.com 通過nginx轉(zhuǎn)發(fā)訪問8080端口服務(wù)
增加一條A記錄
將 A.xxx.com 指向服務(wù)器ip
Nginx配置模板如下:
#path: /usr/local/nginx/conf/nginx.conf
server {
listen 80;
server_name www.xxx.com;
access_log /data/www/log/33.33.33.33_nginx.log combined;
index index.html index.htm index.php;
include /usr/local/nginx/conf/rewrite/none.conf;
root /data/www/website/33.33.33.33:80;
location ~ [^/]\.php(/|$) {
fastcgi_pass unix:/dev/shm/php-cgi.sock;
fastcgi_index index.php;
include fastcgi.conf;
}
location ~ .*\.(gif|jpg|jpeg|png|bmp|swf|flv|ico)$ {
expires 30d;
access_log off;
}
location ~ .*\.(js|css)?$ {
expires 7d;
access_log off;
}
}
server {
listen 80;
listen [::]:80;
server_name A.XXX.com;
proxy_connect_timeout 300s;
proxy_send_timeout 300s;
proxy_read_timeout 300s;
fastcgi_send_timeout 300s;
fastcgi_read_timeout 300s;
location / {
proxy_pass http://127.0.0.1:3000;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
try_files $uri $uri/ =404;
}
}
nginx重新載入配置文件
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。