这篇文章主要介绍“Nginx如何配置多端口多域名访问”,在日常操作中,相信很多人在Nginx如何配置多端口多域名访问问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”Nginx如何配置多端口多域名访问”的疑惑有所帮助!接下来,请跟着小编一起来学习吧!
主域名多端口访问
在dns nameserver设置a记录
将 指向服务器ip
比如我们有两个服务分别开放在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 -s reload
到此,关于“Nginx如何配置多端口多域名访问”的学习就结束了,希望能够解决大家的疑惑。理论与实践的搭配能更好的帮助大家学习,快去试试吧!若想继续学习更多相关知识,请继续关注编程之家网站,小编会继续努力为大家带来更多实用的文章!
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。