Nginx 虚拟主机配置
2022/7/9 5:20:30
本文主要是介绍Nginx 虚拟主机配置,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
为什么会有虚拟主机呢?平时我们用一台服务队外提供服务,但是呢因为用户数及其他原因,导致呢这台服务的cpu、内存、磁盘的使用率不高,但是呢又希望,一台服务器可以提供多个站点,那就需要用到虚拟主机【即多个域名对应一个ip服务器】,如何实现呢? 在http块儿里可以写多个server 但是注意要是 端口号 + 主机名 保证唯一性: 第一种方式配置【同主机名 + 不同端口号】 nginx指定配置文件启动:sudo ./sbin/nginx -c ./conf/nginx_test.conf 【这里的路径都是相对路径是因为进入了nginx目录里】#user nobody; worker_processes 1; events { worker_connections 1024; } http { include mime.types; default_type application/octet-stream; sendfile on; keepalive_timeout 65; # 主站虚拟主机 server { listen 80; # 域名 或 主机名 server_name localhost; location / { root /home/gs/guos/www/www; index index.html index.htm; } error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } } # 视频虚拟主机【vhost】 server { listen 7007; server_name localhost; location / { root /home/gs/guos/www/vod; index index.html index.htm; } error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } } }
命令如下:
第二种方式配置 利用我们在阿里云上申请注册的域名来实现【前提是做好解析并添加记录】【不同域名 + 同一个端口号】#user nobody; worker_processes 1; events { worker_connections 1024; } http { include mime.types; default_type application/octet-stream; sendfile on; keepalive_timeout 65; # 主站虚拟主机 server { listen 80; # 域名 或 主机名 server_name www.jngoodnews.com; location / { root /home/gs/guos/www/www; index index.html index.htm; } error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } } # 视频虚拟主机【vhost】 server { listen 80; server_name vod.jngoodnews.com; location / { root /home/gs/guos/www/vod; index index.html index.htm; } error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } } }
重新加载并检查命令如下:
效果如下:这篇关于Nginx 虚拟主机配置的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-03-29fail2ban nginx
- 2024-03-18install nginx proxy manager
- 2024-01-2444从零开始用Rust编写nginx,命令行参数的设计与解析及说明
- 2024-01-22比Nginx更好用的Gateway!
- 2024-01-17nginx daemon off
- 2023-12-30Nginx Vhost Traffic Status 是什么意思-icode9专业技术文章分享
- 2023-12-26使用nginx搭建creates.io镜像(稀疏索引)
- 2023-11-18centos7编译安装Nginx教程。
- 2023-11-02nginx启动命令和停止命令
- 2023-10-25Centos7系统Nginx负载均衡如何安装和配置?