首頁 > 運維 > Nginx > Nginx如何設定多埠多網域存取

Nginx如何設定多埠多網域存取

PHPz
發布: 2023-05-12 08:43:05
轉載
1835 人瀏覽過

主網域多埠存取

#在dns nameserver設定a記錄

將 指向伺服器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;
  }
}
登入後複製

關鍵就是兩個 server 段配置,你也可以把這兩段拆成兩個配置文件,放到

/etc/nginx/conf.d/
登入後複製

目錄下面;

子網域多埠存取

##這種存取比較傻,因為你的8080連接埠的存取需要http://xxx.com:8080 這樣的格式;

而且如果有兩個不同的cgi,例如80埠對應一個php web服務,8080埠對應一個nodejs web服務;而我們的nodejs自備web服務,已經在8080端口監聽了,這怎麼辦?

這時候我們需要nginx的反向代理功能,並在dns server上面增加一筆a記錄,最後實作

  • www.xxx.com 存取80埠

  • a.xxx.com 透過nginx轉送存取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
登入後複製

以上是Nginx如何設定多埠多網域存取的詳細內容。更多資訊請關注PHP中文網其他相關文章!

相關標籤:
來源:yisu.com
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板