佳木斯湛栽影视文化发展公司

主頁 > 知識庫 > Nginx配置多端口多域名訪問的實現(xiàn)

Nginx配置多端口多域名訪問的實現(xiàn)

熱門標簽:美圖手機 硅谷的囚徒呼叫中心 使用U盤裝系統(tǒng) 網(wǎng)站建設 百度競價點擊價格的計算公式 阿里云 智能手機 檢查注冊表項

在一個服務器上部署多個站點,需要開放多個端口來訪問不同的站點,流程很簡單,調(diào)試花了2小時,記錄一下:

主域名多端口訪問

在DNS NameServer設置A記錄

將 www.xxx.com 指向服務器ip

開放所需端口,修改nginx配置文件

比如我們有兩個服務分別開放在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)鍵就是兩個 server 段配置,你也可以把這兩段拆成兩個配置文件,放到

/etc/nginx/conf.d/

目錄下面;

子域名多端口訪問

這種訪問比較傻,因為你的8080端口的訪問需要 http://xxx.com:8080 這樣的格式;

而且如果有兩個不同的cgi,比如80端口對應一個php web服務, 8080端口對應一個nodejs web服務;而我們的nodejs自帶web服務,已經(jīng)在8080端口監(jiān)聽了,這怎么辦?

這個時候我們需要Nginx的反向代理功能,并在DNS Server上面增加一條A記錄,最終實現(xiàn)

  • www.xxx.com 訪問80端口
  • A.xxx.com 通過nginx轉(zhuǎn)發(fā)訪問8080端口服務

增加一條A記錄

將 A.xxx.com 指向服務器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重新載入配置文件

nginx -s reload

以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。

標簽:山南 黃山 湘潭 湖北 懷化 賀州 通遼 煙臺

巨人網(wǎng)絡通訊聲明:本文標題《Nginx配置多端口多域名訪問的實現(xiàn)》,本文關(guān)鍵詞  ;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請?zhí)峁┫嚓P(guān)信息告之我們,我們將及時溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡,涉及言論、版權(quán)與本站無關(guān)。
  • 相關(guān)文章
  • 收縮
    • 微信客服
    • 微信二維碼
    • 電話咨詢

    • 400-1100-266
    通江县| 喀喇沁旗| 伊通| 宝山区| 六安市| 泾阳县| 涡阳县| 张家川| 喜德县| 义乌市| 玛沁县| 阿拉善左旗| 宁都县| 三台县| 梁平县| 新绛县| 嘉禾县| 蒲江县| 鹤山市| 肇州县| 红原县| 新竹市| 珲春市| 谷城县| 尉犁县| 贡觉县| 巴塘县| 浏阳市| 乌兰县| 汝南县| 淮阳县| 松阳县| 柳河县| 车险| 乐陵市| 岳普湖县| 阿城市| 定日县| 屏东县| 大城县| 额尔古纳市|