溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

Nginx如何配置多端口多域名訪問

發(fā)布時間:2022-06-02 14:32:38 來源:億速云 閱讀:161 作者:iii 欄目:大數(shù)據(jù)

這篇文章主要介紹“Nginx如何配置多端口多域名訪問”,在日常操作中,相信很多人在Nginx如何配置多端口多域名訪問問題上存在疑惑,小編查閱了各式資料,整理出簡單好用的操作方法,希望對大家解答”Nginx如何配置多端口多域名訪問”的疑惑有所幫助!接下來,請跟著小編一起來學習吧!

主域名多端口訪問

dns nameserver設(shè)置a記錄

將  指向服務(wù)器ip

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

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

/etc/nginx/conf.d/

目錄下面;

子域名多端口訪問

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

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

這個時候我們需要nginx的反向代理功能,并在dns server上面增加一條a記錄,最終實現(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重新載入配置文件

nginx -s reload

到此,關(guān)于“Nginx如何配置多端口多域名訪問”的學習就結(jié)束了,希望能夠解決大家的疑惑。理論與實踐的搭配能更好的幫助大家學習,快去試試吧!若想繼續(xù)學習更多相關(guān)知識,請繼續(xù)關(guān)注億速云網(wǎng)站,小編會繼續(xù)努力為大家?guī)砀鄬嵱玫奈恼拢?/p>

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI