首页 运维 nginx nginx怎么配置二级域名

nginx怎么配置二级域名

May 27, 2023 pm 05:37 PM
nginx

我的vps挂了三个服务, 分别是:

  1. wordpress搭建的博客服务, 运行于8000端口, 访问方式 http://fangyuanxiaozhan.com:8000

  2. gogs搭建的git服务, 运行于10080端口, 访问方式 http://fangyuanxiaozhan.com:10080

  3. nextcloud搭建的网盘服务, 运行于8080端口, 访问方式 http://fangyuanxiaozhan.com:10080

我的需求:

  1. 1.访问博客服务时, 直接输入 http://fangyuanxiaozhan.com

  2. 访问git服务时, 直接输入 http://git.fangyuanxiaozhan.com

  3. 访问网盘服务时, 直接输入 http://cloud.fangyuanxiaozhan.com

实现的方法

1、到托管域名的网站, 添加dns解析, 我的域名 fangyuanxiaozhan.com 托管在阿里云, 我的做法是登录 https://dns.console.aliyun.com/#/dns/domainlist , 添加二级记录

nginx怎么配置二级域名 

2、我使用的是centos7, nginx配置文件的默认位置为 /etc/nginx/nginx.conf , 有意思的是, /etc/nginx/nginx.conf 内引入了 配置文件夹 /etc/nginx/conf.d , 也就是我们可以把 /etc/nginx/nginx.conf 中的一些默认配置注释掉, 直接在文件夹 /etc/nginx/conf.d 中配置多个独立的配置文件.

nginx怎么配置二级域名 

/etc/nginx/nginx.conf 的配置

# for more information on configuration, see:
#  * official english documentation: http://nginx.org/en/docs/
#  * official russian documentation: http://nginx.org/ru/docs/

user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;

# load dynamic modules. see /usr/share/nginx/readme.dynamic.
include /usr/share/nginx/modules/*.conf;

events {
  worker_connections 1024;
}

http {
  log_format main '$remote_addr - $remote_user [$time_local] "$request" '
           '$status $body_bytes_sent "$http_referer" '
           '"$http_user_agent" "$http_x_forwarded_for"';

  access_log /var/log/nginx/access.log main;

  sendfile      on;
  tcp_nopush     on;
  tcp_nodelay     on;
  keepalive_timeout  65;
  types_hash_max_size 2048;

  include       /etc/nginx/mime.types;
  default_type    application/octet-stream;

  include /etc/nginx/conf.d/*.conf;

}
登录后复制

注意上述配置文件的最后一行, include /etc/nginx/conf.d/*.conf; 保证了 /etc/nginx/conf.d/ 下,所有以.conf结尾的配置文件, 都会被主配置文件 nginx.conf 引入并生效

/etc/nginx/conf.d/ 下面需要新建三个文件

nginx怎么配置二级域名

blog.conf (实现8000端口映射到80端口, 不使用二级域名)

server { 
  listen 80;
  server_name fangyuanxiaozhan.com;

  location / {
    proxy_set_header  x-real-ip $remote_addr;
    proxy_set_header  host   $http_host;
    proxy_pass     http://0.0.0.0:8000;
  }
}
登录后复制

blog.conf实现了fangyuanxiaozhan.com:8000映射到 fangyuanxiaozhan.com

git.conf (实现10080端口映射到80端口, 使用二级域名 git )

server { 
  listen 80;
  server_name git.fangyuanxiaozhan.com;

  location / {
    proxy_set_header  x-real-ip $remote_addr;
    proxy_set_header  host   $http_host;
    proxy_pass     http://0.0.0.0:10080;
  }
}
登录后复制

git.conf实现了fangyuanxiaozhan.com:10080映射到 git.fangyuanxiaozhan.com

nc.conf (实现10080端口映射到80端口, 使用二级域名 cloud )

server { 
  listen 80;
  server_name cloud.fangyuanxiaozhan.com;

  location / {
    proxy_set_header  x-real-ip $remote_addr;
    proxy_set_header  host   $http_host;
    proxy_pass     http://0.0.0.0:8080;
  }
}
登录后复制

git.conf实现了fangyuanxiaozhan.com:8080映射到 cloud.fangyuanxiaozhan.com

重启nginx使配置生效

关闭nginx

sudo $(which nginx) -s stop
登录后复制

开启nginx

sudo $(which nginx)
登录后复制

效果展示

nginx怎么配置二级域名 

nginx怎么配置二级域名 

nginx怎么配置二级域名 

以上是nginx怎么配置二级域名的详细内容。更多信息请关注PHP中文网其他相关文章!

本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn

热AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover

AI Clothes Remover

用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool

Undress AI Tool

免费脱衣服图片

Clothoff.io

Clothoff.io

AI脱衣机

AI Hentai Generator

AI Hentai Generator

免费生成ai无尽的。

热工具

记事本++7.3.1

记事本++7.3.1

好用且免费的代码编辑器

SublimeText3汉化版

SublimeText3汉化版

中文版,非常好用

禅工作室 13.0.1

禅工作室 13.0.1

功能强大的PHP集成开发环境

Dreamweaver CS6

Dreamweaver CS6

视觉化网页开发工具

SublimeText3 Mac版

SublimeText3 Mac版

神级代码编辑软件(SublimeText3)

怎么查看nginx是否启动 怎么查看nginx是否启动 Apr 14, 2025 pm 01:03 PM

确认 Nginx 是否启动的方法:1. 使用命令行:systemctl status nginx(Linux/Unix)、netstat -ano | findstr 80(Windows);2. 检查端口 80 是否开放;3. 查看系统日志中 Nginx 启动消息;4. 使用第三方工具,如 Nagios、Zabbix、Icinga。

nginx怎么配置云服务器域名 nginx怎么配置云服务器域名 Apr 14, 2025 pm 12:18 PM

在云服务器上配置 Nginx 域名的方法:创建 A 记录,指向云服务器的公共 IP 地址。在 Nginx 配置文件中添加虚拟主机块,指定侦听端口、域名和网站根目录。重启 Nginx 以应用更改。访问域名测试配置。其他注意事项:安装 SSL 证书启用 HTTPS、确保防火墙允许 80 端口流量、等待 DNS 解析生效。

怎么启动nginx服务器 怎么启动nginx服务器 Apr 14, 2025 pm 12:27 PM

启动 Nginx 服务器需要按照不同操作系统采取不同的步骤:Linux/Unix 系统:安装 Nginx 软件包(例如使用 apt-get 或 yum)。使用 systemctl 启动 Nginx 服务(例如 sudo systemctl start nginx)。Windows 系统:下载并安装 Windows 二进制文件。使用 nginx.exe 可执行文件启动 Nginx(例如 nginx.exe -c conf\nginx.conf)。无论使用哪种操作系统,您都可以通过访问服务器 IP

linux怎么启动nginx linux怎么启动nginx Apr 14, 2025 pm 12:51 PM

在 Linux 中启动 Nginx 的步骤:检查 Nginx 是否已安装。使用 systemctl start nginx 启动 Nginx 服务。使用 systemctl enable nginx 启用在系统启动时自动启动 Nginx。使用 systemctl status nginx 验证启动是否成功。在 Web 浏览器中访问 http://localhost 查看默认欢迎页面。

linux怎么查看nginx是否启动 linux怎么查看nginx是否启动 Apr 14, 2025 pm 12:48 PM

在 Linux 中,使用以下命令检查 Nginx 是否已启动:systemctl status nginx根据命令输出进行判断:如果显示 "Active: active (running)",则 Nginx 已启动。如果显示 "Active: inactive (dead)",则 Nginx 已停止。

nginx怎么查版本 nginx怎么查版本 Apr 14, 2025 am 11:57 AM

可以查询 Nginx 版本的方法有:使用 nginx -v 命令;查看 nginx.conf 文件中的 version 指令;打开 Nginx 错误页,查看页面的标题。

nginx在windows中怎么配置 nginx在windows中怎么配置 Apr 14, 2025 pm 12:57 PM

如何在 Windows 中配置 Nginx?安装 Nginx 并创建虚拟主机配置。修改主配置文件并包含虚拟主机配置。启动或重新加载 Nginx。测试配置并查看网站。选择性启用 SSL 并配置 SSL 证书。选择性设置防火墙允许 80 和 443 端口流量。

nginx403怎么解决 nginx403怎么解决 Apr 14, 2025 am 10:33 AM

如何解决 Nginx 403 Forbidden 错误?检查文件或目录权限;2. 检查 .htaccess 文件;3. 检查 Nginx 配置文件;4. 重启 Nginx。其他可能原因还包括防火墙规则、SELinux 设置或应用程序问题。

See all articles