【Linux】Nginx一个域名https&一个地址配置多个项目【项目实战】

👨‍🎓博主简介

  🏅CSDN博客专家

  🏅云计算领域优质创作者

  🏅华为云开发者社区专家博主

  🏅阿里云开发者社区专家博主

💊交流社区: 运维交流社区 欢迎大家的加入!

🐋 希望大家多多支持,我们一起进步!😄

🎉如果文章对你有帮助的话,欢迎 点赞 👍🏻 评论 💬 收藏 ⭐️ 加关注+💗


文章目录

一个域名带https配置多个项目

①、首先将项目移动到html下;

②、将ssl证书移动到目的地;

③、然后进行nginx配置;

Nginx配置文件完整代码展示(主要在server段):

bash 复制代码
#user  root;
worker_processes 1;

events {
  worker_connections 1024;
}


http {
  include mime.types;
  default_type application/octet-stream;

  sendfile on;
  #tcp_nopush     on;

  #keepalive_timeout  0;
  keepalive_timeout 65;

  #gzip  on;

  server {
    listen 80;
    server_name test.top www.test.top;

    # HTTP 重定向到 HTTPS
    return 301 https://$host$request_uri;
  }

  server {
    listen 443 ssl;
    server_name test.top www.test.top;

    # SSL 配置
    ssl_certificate /usr/local/nginx/conf/ssl/test.top.pem;
    ssl_certificate_key /usr/local/nginx/conf/ssl/test.top.key;
    ssl_protocols TLSv1.2 TLSv1.3;
    ssl_ciphers 'TLS_AES_128_GCM_SHA256:TLS_AES_256_GCM_SHA384:ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256';
    ssl_prefer_server_ciphers on;

    # 设置错误页面
    error_page 404 /404.html;
    error_page 500 /500.html;

    location = /404.html {
      root /usr/local/nginx/html;
      internal;
    }

    location = /500.html {
      root /usr/local/nginx/html;
      internal;
    }

    # 定义项目根目录
    root /usr/local/nginx/html;

    # 默认访问 项目1 【如果不想用项目1,也可以自定义其他项目】
    location / {
      try_files $uri $uri/ /项目1/index.html;
      #			root   html;
      #			index  index.html index.htm;
    }

    # 项目1 项目
    location /项目1 {
      alias /usr/local/nginx/html/项目1;
      try_files $uri $uri/ /项目1/index.html;
    }

    # 项目2 项目
    location /项目2 {
      alias /usr/local/nginx/html/项目2;
      try_files $uri $uri/ /项目2/index.html;
    }

    # 项目3 项目
    location /项目3 {
      alias /usr/local/nginx/html/项目3;
      try_files $uri $uri/ /项目3/index.html;
    }
  }
}

这样页面访问时就可以是:https://test.top/项目1/、https://test.top/项目2/、https://test.top/项目3/。

一个域名配置多个项目

①、首先将项目移动到html下;

②、然后进行nginx配置;

Nginx配置文件完整代码展示(主要在server段):

bash 复制代码
#user  root;
worker_processes 1;

events {
  worker_connections 1024;
}


http {
  include mime.types;
  default_type application/octet-stream;

  sendfile on;
  #tcp_nopush     on;

  #keepalive_timeout  0;
  keepalive_timeout 65;

  #gzip  on;

  server {
    listen 80;
    server_name test.top www.test.top;

    # 设置错误页面
    error_page 404 /404.html;
    error_page 500 /500.html;

    location = /404.html {
      root /usr/local/nginx/html;
      internal;
    }

    location = /500.html {
      root /usr/local/nginx/html;
      internal;
    }

    # 定义项目根目录
    root /usr/local/nginx/html;

    # 默认访问 项目1 【如果不想用项目1,也可以自定义其他项目】
    location / {
      try_files $uri $uri/ /项目1/index.html;
      #			root   html;
      #			index  index.html index.htm;
    }

    # 项目1 项目
    location /项目1 {
      alias /usr/local/nginx/html/项目1;
      try_files $uri $uri/ /项目1/index.html;
    }

    # 项目2 项目
    location /项目2 {
      alias /usr/local/nginx/html/项目2;
      try_files $uri $uri/ /项目2/index.html;
    }

    # 项目3 项目
    location /项目3 {
      alias /usr/local/nginx/html/项目3;
      try_files $uri $uri/ /项目3/index.html;
    }
  }
}

这样页面访问时就可以是:http://test.top/项目1/、http://test.top/项目2/、http://test.top/项目3/。

本机地址配置多个项目

本机地址配置多个项目这个其实和一个域名配置多个项目是一样的,只不过把server_name的域名换成localhost就行。

具体操作如下:

①、首先将项目移动到html下;

②、然后进行nginx配置;

Nginx配置文件完整代码展示(主要在server段):

conf 复制代码
user root;
worker_processes 1;

events {
  worker_connections 1024;
}


http {
  include mime.types;
  default_type application/octet-stream;

  sendfile on;
  #tcp_nopush     on;

  #keepalive_timeout  0;
  keepalive_timeout 65;

  #gzip  on;

  server {
    listen 80;
    server_name localhost;

    # 设置错误页面
    error_page 404 /404.html;
    error_page 500 /500.html;

    location = /404.html {
      root /usr/local/nginx/html;
      internal;
    }

    location = /500.html {
      root /usr/local/nginx/html;
      internal;
    }

    # 定义项目根目录
    root /usr/local/nginx/html;

    # 默认访问 html/下的index.html页面
    location / {
      root html;
      index index.html index.htm;
      # 如果需要以项目1为默认页,可以把这个配置打开,上面的两个注释了就行;
      # try_files $uri $uri/ /项目1/index.html;
    }

    # 项目1 项目
    location /项目1 {
      alias /usr/local/nginx/html/项目1;
      try_files $uri $uri/ /项目1/index.html;
    }

    # 项目2 项目
    location /项目2 {
      alias /usr/local/nginx/html/项目2;
      try_files $uri $uri/ /项目2/index.html;
    }
  }
}

这样页面访问的是时候就可以是:ip/项目1/、ip/项目2/。

推荐一个优化nginx配置文件的页面:Nginx配置文件格式化

这里面还有很多格式化工具可以自己看看:https://tool.okcode.vip/

Nginx 404页面美化:Nginx 404页面美化

相关文章

文章标题 文章连接
【Linux】nginx基础篇 -- 介绍及yum安装nginx https://liucy.blog.csdn.net/article/details/133928000
【Linux】环境下部署Nginx服务 - 二进制部署方式 https://liucy.blog.csdn.net/article/details/132145067
nginx配置负载均衡--实战项目(适用于轮询、加权轮询、ip_hash) https://liucy.blog.csdn.net/article/details/133986013
nginx快速部署一个网站服务 + 多域名 + 多端口 https://liucy.blog.csdn.net/article/details/133986102
【Linux】Nginx一个域名https&一个地址配置多个项目【项目实战】 https://liucy.blog.csdn.net/article/details/144442148

相关专栏

❀《Linux从入门到精通》专栏 ❀
❀《Nginx》专栏 ❀
🐋 希望大家多多支持,我们一起进步!😄

🎉如果文章对你有帮助的话,欢迎 点赞 👍🏻 评论 💬 收藏 ⭐️ 加关注+💗

相关推荐
Lv_沐曦1 分钟前
Linux 上的Web文件管理器-filebrowser
linux·运维·服务器
闲云自留地7 分钟前
CentOS 搬 Ubuntu 24.04:yum 变 apt、防火墙、SELinux、root 全差异避坑
linux·ubuntu·centos
科技每日热闻12 分钟前
电气设备综合维保服务商怎么选?从预防性维护到数字化运维,看懂五个关键能力——企业选择维保合作伙伴,不能只看“坏了能不能修”,更要看能否覆盖资产与系统的长期运营
大数据·运维·人工智能
最强小杰1 小时前
Claude Opus 4.8 调用报 529 怎么办?并发限制触发过载——退避策略与降级方案完整指南
运维·网络·python·ai
我科绝伦(Huanhuan Zhou)1 小时前
磐维数据库(PanWeiDB)日常运维命令总结
运维·数据库
深盾科技_Virbox1 小时前
软许可、加密锁和云许可混合使用时,授权接口应该怎么统一?
运维·网络·安全
逐流人1 小时前
Redis内存数据库:从数据类型到集群部署
linux·运维·数据库·redis·缓存·云计算
北风toto1 小时前
Windows 下用 OpenSSL 生成 SSL 密钥并配置 Nginx 教程
windows·nginx·ssl
。1 小时前
【无标题】
linux·服务器·负载均衡·lvs
励志师code1 小时前
Kubernetes 集群节点生命周期管理实战:从扩容、调度管控到证书续期
运维·docker·容器·kubernetes·gpu算力·kubelet