当前位置:首页 > 技术文章 > 正文内容

nginx配置多个站点使用同一端口

arlanguage5个月前 (12-09)技术文章50

场景描述

用户在同一台服务器上部署了多个站点,需要使用不同域名的80端口来访问不同的网站

准备事项

  • 主机已安装好nginx服务
  • 站点文件已上传到主机目录
  • 不同域名已解析到主机IP

部署步骤

1、检查/etc/nginx/nginx.conf <span style="color:red">(nginx默认配置文件路径,如果使用其他方式部署,对应部署的其他路径)</span>配置文件,确保文件中包含 include /etc/nginx/conf.d/*.conf;

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

# Load dynamic modules. See /usr/share/doc/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;

    # Load modular configuration files from the /etc/nginx/conf.d directory.
    # See http://nginx.org/en/docs/ngx_core_module.html#include
    # for more information.
    include /etc/nginx/conf.d/*.conf;  #nginx站点配置文件路径

    server {
        listen       80 default_server; #默认监听端口
        listen       [::]:80 default_server;
        server_name  _;
        root         /usr/share/nginx/html; #默认站点路径

        # Load configuration files for the default server block.
        include /etc/nginx/default.d/*.conf;

        location / {
        }

        error_page 404 /404.html;
        location = /404.html {
        }

        error_page 500 502 503 504 /50x.html;
        location = /50x.html {
        }
    }
}

2、在目录/etc/nginx/conf.d下面新建website1.conf,website2.conf,文件名可自定义。监听端口都改为80端口并绑定不同的域名,root路径分别配置为不同的站点路径。 website1.conf文件内容:

server {
    listen       80;	#website1监听端口
    server_name  one.example.com;	#website1绑定域名
    root         /var/www/html/web1/;	#website1站点路径
    # Load configuration files for the default server block.

    location / {
        index index.php index.html index.htm;	#站点默认页面
        }
    error_page 404 /404.html;
        location = /40x.html {
    }
    error_page 500 502 503 504 /50x.html;
        location = /50x.html {
    }
}

website2.conf文件内容:

server {
    listen       80;	#website2监听端口
    server_name  two.example.com;	#website2绑定域名
    root         /var/www/html/web2/;	#website2站点路径
    # Load configuration files for the default server block.

    location / {
        index index.php index.html index.htm;	#站点默认页面
        }
    error_page 404 /404.html;
        location = /40x.html {
    }
    error_page 500 502 503 504 /50x.html;
        location = /50x.html {
    }
}

3、重启nginx服务,验证不同域名访问效果

扫描二维码推送至手机访问。

版权声明:本文由AR编程网发布,如需转载请注明出处。

本文链接:http://www.arlanguage.com/post/28.html

分享给朋友:

“nginx配置多个站点使用同一端口” 的相关文章

CentOS安装nginx

安装nginx第一步,查看是否安装,没有我们就开始安装:rpm -qa| grep nginx第二步,安装依赖包。(1)gcc安装。由于nginx是c语言写的,因此在安装nginx前需要先将官网下载的源码进行编译,编译依赖gcc环境,如果没有gcc环境,则需要安装:yum install gcc-c...

全网最新最全的Docker命令大集合,收藏备用!

Docker 是一个开源的平台,用于自动化应用程序的部署、扩展和管理。它使得开发者能够打包应用及其依赖项到一个轻量级的容器中,并在任何环境中快速运行。掌握 Docker 的常用命令可以大大提高你的工作效率,尤其是在开发、测试和部署阶段。本文将详细介绍 Docker 的常用命令,涵盖从基本操作到高级使...

WordPress切换到Nginx服务器教程

这几天将几个WordPress的Web服务器从Apache切换到了Nginx,中间遇到了不少问题,因此记录一下,以便日后维护使用。对于WordPress站点来说,固定链接主要是通过根目录下的.htaccess文件来控制,切换服务器后,Nginx的rewrite格式和Apache的不同,需要修改。先卸...

为什么一条UPDATE语句有索引反而更慢

先来看看今天要讲的主人翁:UPDATE `i_msg_system` set `deliver`=1 where `uid`=10000 and `msg_group`=0 and `deliver`=0;涉事表结构:CREATE TABLE `i_msg_system` ( `id` int(...

Linux 网络延迟排查方法 linux延迟10秒

在 Linux 服务器中,可以通过内核调优、DPDK 以及 XDP 等多种方式提高服务器的抗攻击能力,降低 DDoS 对正常服务的影响。在应用程序中,可以使用各级缓存、WAF、CDN 等来缓解 DDoS 对应用程序的影响。但是需要注意的是,如果 DDoS 流量已经到达 Linux 服务器,那么即使应...

Nuxt.js从0到1之入门教程

题外话:近来想把网站做下更新,从框架到内容的更新,但又不想放弃SEO的优势,日常工作用到vue.js在SEO方面劣势较多,果断选择了Nuxt.js,苦于国内文章抄袭的非常严重 如某n 某书,最终在稀土找到一篇像样的文章,以头条为平台记录网站改版全过程。此文章建议在PC端查看,涉及到代码部分,移动端查...