Nginx配置多端口多域名訪問(wèn)的實(shí)現(xiàn)
在一個(gè)服務(wù)器上部署多個(gè)站點(diǎn),需要開(kāi)放多個(gè)端口來(lái)訪問(wèn)不同的站點(diǎn),流程很簡(jiǎn)單,調(diào)試花了2小時(shí),記錄一下:
主域名多端口訪問(wèn)
在DNS NameServer設(shè)置A記錄
將 www.xxx.com 指向服務(wù)器ip
開(kāi)放所需端口,修改nginx配置文件
比如我們有兩個(gè)服務(wù)分別開(kāi)放在80端口和8080端口
如果有iptable,先開(kāi)放端口:
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è)配置文件,放到
/etc/nginx/conf.d/
目錄下面;
子域名多端口訪問(wèn)
這種訪問(wèn)比較傻,因?yàn)槟愕?080端口的訪問(wèn)需要 http://xxx.com:8080 這樣的格式;
而且如果有兩個(gè)不同的cgi,比如80端口對(duì)應(yīng)一個(gè)php web服務(wù), 8080端口對(duì)應(yīng)一個(gè)nodejs web服務(wù);而我們的nodejs自帶web服務(wù),已經(jīng)在8080端口監(jiān)聽(tīng)了,這怎么辦?
這個(gè)時(shí)候我們需要Nginx的反向代理功能,并在DNS Server上面增加一條A記錄,最終實(shí)現(xiàn)
- www.xxx.com 訪問(wèn)80端口
- A.xxx.com 通過(guò)nginx轉(zhuǎn)發(fā)訪問(wèn)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
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持本站。
版權(quán)聲明:本站文章來(lái)源標(biāo)注為YINGSOO的內(nèi)容版權(quán)均為本站所有,歡迎引用、轉(zhuǎn)載,請(qǐng)保持原文完整并注明來(lái)源及原文鏈接。禁止復(fù)制或仿造本網(wǎng)站,禁止在非www.sddonglingsh.com所屬的服務(wù)器上建立鏡像,否則將依法追究法律責(zé)任。本站部分內(nèi)容來(lái)源于網(wǎng)友推薦、互聯(lián)網(wǎng)收集整理而來(lái),僅供學(xué)習(xí)參考,不代表本站立場(chǎng),如有內(nèi)容涉嫌侵權(quán),請(qǐng)聯(lián)系alex-e#qq.com處理。