Nginx配置多端口多域名訪問的實現(xiàn)
在一個服務(wù)器上部署多個站點,需要開放多個端口來訪問不同的站點,流程很簡單,調(diào)試花了2小時,記錄一下:
主域名多端口訪問
在DNS NameServer設(shè)置A記錄
將 www.xxx.com 指向服務(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端口對應(yīng)一個php web服務(wù), 8080端口對應(yīng)一個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
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
docker nginx實現(xiàn)一個主機部署多個站點操作
這篇文章主要介紹了docker nginx實現(xiàn)一個主機部署多個站點操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-11-11nginx配置完rewrite瀏覽器提示將您重定向的次數(shù)過多的解決方法
本文主要介紹了nginx配置完rewrite瀏覽器提示將您重定向的次數(shù)過多的解決方法,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-07-07nginx+tomcat實現(xiàn)負(fù)載均衡,使用redis session共享
這篇文章主要介紹了nginx tomcat負(fù)載均衡 使用redis session共享,有興趣的同學(xué)可以了解一下。2016-12-12