Nginx配置多端口多域名訪問的實現(xiàn)
在一個服務器上部署多個站點,需要開放多個端口來訪問不同的站點,流程很簡單,調(diào)試花了2小時,記錄一下:
主域名多端口訪問
在DNS NameServer設(shè)置A記錄
將 www.xxx.com 指向服務器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; } }
關(guān)鍵就是兩個 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轉(zhuǎn)發(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
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
keepalived+nginx高可用實現(xiàn)方法示例
這篇文章主要介紹了keepalived+nginx高可用實現(xiàn)方法示例,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2019-05-05通過lua來配置實現(xiàn)Nginx服務器的防盜鏈功能
這篇文章主要介紹了通過lua來配置實現(xiàn)Nginx服務器的防盜鏈功能的方法,這里主要講解生成鏈接的Nginx配置,需要的朋友可以參考下2016-01-01Nginx HTTP:413 Request Entity Too Large解決方法
這篇文章主要介紹了Nginx HTTP:413 Request Entity Too Large解決方法,這個問題需要修改PHP配置以及Nginx配置才可以解決,需要的朋友可以參考下2015-07-07Nginx配置多個端口進行監(jiān)聽的實現(xiàn)
隨著容器的應用越來越多,將nginx部署在容器中也是常有之事,本文主要介紹了Nginx配置多個端口進行監(jiān)聽的實現(xiàn),文中通過示例代碼介紹的非常詳細,需要的朋友們下面隨著小編來一起學習學習吧2024-07-07