新聞中心
這篇文章主要介紹“Nginx如何配置多端口多域名訪問”,在日常操作中,相信很多人在Nginx如何配置多端口多域名訪問問題上存在疑惑,小編查閱了各式資料,整理出簡單好用的操作方法,希望對大家解答”Nginx如何配置多端口多域名訪問”的疑惑有所幫助!接下來,請跟著小編一起來學習吧!
在邊壩等地區(qū),都構建了全面的區(qū)域性戰(zhàn)略布局,加強發(fā)展的系統(tǒng)性、市場前瞻性、產(chǎn)品創(chuàng)新能力,以專注、極致的服務理念,為客戶提供成都網(wǎng)站設計、成都網(wǎng)站建設 網(wǎng)站設計制作按需搭建網(wǎng)站,公司網(wǎng)站建設,企業(yè)網(wǎng)站建設,品牌網(wǎng)站設計,全網(wǎng)營銷推廣,成都外貿(mào)網(wǎng)站建設,邊壩網(wǎng)站建設費用合理。
主域名多端口訪問
在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服務,已經(jīng)在8080端口監(jiān)聽了,這怎么辦?
這個時候我們需要nginx的反向代理功能,并在dns server上面增加一條a記錄,最終實現(xiàn)
www.xxx.com 訪問80端口
a.xxx.com 通過nginx轉發(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
到此,關于“Nginx如何配置多端口多域名訪問”的學習就結束了,希望能夠解決大家的疑惑。理論與實踐的搭配能更好的幫助大家學習,快去試試吧!若想繼續(xù)學習更多相關知識,請繼續(xù)關注創(chuàng)新互聯(lián)網(wǎng)站,小編會繼續(xù)努力為大家?guī)砀鄬嵱玫奈恼拢?/p>
網(wǎng)頁標題:Nginx如何配置多端口多域名訪問
標題路徑:http://www.ef60e0e.cn/article/jdojee.html