Nginx——虚拟主机简介

本文涉及的产品
.cn 域名,1个 12个月
简介: Nginx——虚拟主机简介

前言

如题,简单的说下三种虚拟主机,IP虚机主机,端口虚拟主机,域名虚拟主机

内容

什么是NGINX虚拟主机

Nginx服务部署在一台服务器上,通过IP端口域名对外实现多个访问入口,让客户端以为是多个服务器,这就是nginx虚拟主机。

常见的虚拟主机都是基于域名的虚拟主机

基于IP的虚拟主机配置

一台NGINX服务器绑定多个ip,访问不同的IP请求不同的目录。

IP-1配置

######################## IP-1 ############################
  server {
    listen 192.168.223.21:80;
    server_name _;
    access_log /data/wwwlogs/access_nginx_IP_1.log combined;
    root /data/wwwroot/21;
    index index.html index.htm index.php;
    #error_page 404 /404.html;
    #error_page 502 /502.html;
    location /nginx_status {
      stub_status on;
      access_log off;
      allow 127.0.0.1;
      deny all;
    }
    location ~ [^/]\.php(/|$) {
      #fastcgi_pass remote_php_ip:9000;
      fastcgi_pass unix:/dev/shm/php-cgi.sock;
      fastcgi_index index.php;
      include fastcgi.conf;
    }
    location ~ .*\.(gif|jpg|jpeg|png|bmp|swf|flv|mp4|ico)$ {
      expires 30d;
      access_log off;
    location ~ [^/]\.php(/|$) {
      #fastcgi_pass remote_php_ip:9000;
      fastcgi_pass unix:/dev/shm/php-cgi.sock;
      fastcgi_index index.php;
      include fastcgi.conf;
    }
    location ~ .*\.(gif|jpg|jpeg|png|bmp|swf|flv|mp4|ico)$ {
      expires 30d;
      access_log off;
    }
    location ~ .*\.(js|css)?$ {
      expires 7d;
      access_log off;
    }
    location ~ ^/(\.user.ini|\.ht|\.git|\.svn|\.project|LICENSE|README.md) {
      deny all;
    }
  }

IP-2配置

######################## IP-1 ############################
  server {
    listen 192.168.223.22:80;
    server_name _;
    access_log /data/wwwlogs/access_nginx_IP_2.log combined;
    root /data/wwwroot/22;
    index index.html index.htm index.php;
    #error_page 404 /404.html;
    #error_page 502 /502.html;
    location /nginx_status {
      stub_status on;
      access_log off;
      allow 127.0.0.1;
      deny all;
    }
    location ~ [^/]\.php(/|$) {
      #fastcgi_pass remote_php_ip:9000;
      fastcgi_pass unix:/dev/shm/php-cgi.sock;
      fastcgi_index index.php;
      include fastcgi.conf;
    }
    location ~ .*\.(gif|jpg|jpeg|png|bmp|swf|flv|mp4|ico)$ {
      expires 30d;
      access_log off;
    location ~ [^/]\.php(/|$) {
      #fastcgi_pass remote_php_ip:9000;
      fastcgi_pass unix:/dev/shm/php-cgi.sock;
      fastcgi_index index.php;
      include fastcgi.conf;
    }
    location ~ .*\.(gif|jpg|jpeg|png|bmp|swf|flv|mp4|ico)$ {
      expires 30d;
      access_log off;
    }
    location ~ .*\.(js|css)?$ {
      expires 7d;
      access_log off;
    }
    location ~ ^/(\.user.ini|\.ht|\.git|\.svn|\.project|LICENSE|README.md) {
      deny all;
    }
  }

基于端口虚拟主机配置

一台NGINX服务器绑定多个端口,访问不同的端口请求不同的目录。

Port-1配置

######################## Port-1 ############################
  server {
    listen 80;
    server_name _;
    access_log /data/wwwlogs/access_nginx_Prot_1.log combined;
    root /data/wwwroot/80;
    index index.html index.htm index.php;
    #error_page 404 /404.html;
    #error_page 502 /502.html;
    location /nginx_status {
      stub_status on;
      access_log off;
      allow 127.0.0.1;
      deny all;
    }
    location ~ [^/]\.php(/|$) {
      #fastcgi_pass remote_php_ip:9000;
      fastcgi_pass unix:/dev/shm/php-cgi.sock;
      fastcgi_index index.php;
      include fastcgi.conf;
    }
    location ~ .*\.(gif|jpg|jpeg|png|bmp|swf|flv|mp4|ico)$ {
      expires 30d;
      access_log off;
    }
    location ~ .*\.(js|css)?$ {
      expires 7d;
      access_log off;
    }
    location ~ ^/(\.user.ini|\.ht|\.git|\.svn|\.project|LICENSE|README.md) {
      deny all;
    }
  }

Port-2配置

######################## Port-2 ############################
  server {
    listen 8080;
    server_name _;
    access_log /data/wwwlogs/access_nginx_Port_2.log combined;
    root /data/wwwroot/8080;
    index index.html index.htm index.php;
    #error_page 404 /404.html;
    #error_page 502 /502.html;
    location /nginx_status {
      stub_status on;
      access_log off;
      allow 127.0.0.1;
      deny all;
    }
    location ~ [^/]\.php(/|$) {
      #fastcgi_pass remote_php_ip:9000;
      fastcgi_pass unix:/dev/shm/php-cgi.sock;
      fastcgi_index index.php;
      include fastcgi.conf;
    }
    location ~ .*\.(gif|jpg|jpeg|png|bmp|swf|flv|mp4|ico)$ {
      expires 30d;
      access_log off;
    }
    location ~ .*\.(js|css)?$ {
      expires 7d;
      access_log off;
    }
    location ~ ^/(\.user.ini|\.ht|\.git|\.svn|\.project|LICENSE|README.md) {
      deny all;
    }
  }

基于域名虚拟主机配置

两个域名指向同一Nginx,访问不同的域名请求不同的目录。

www.a.com配置

server {
  listen 80;
  listen [::]:80;
  server_name www.a.com;
  access_log /data/wwwlogs/www.a.com_nginx.log combined;
  index index.html index.htm index.php;
  root /data/wwwroot/www.a.com;
  
  include /usr/local/nginx/conf/rewrite/none.conf;
  #error_page 404 /404.html;
  #error_page 502 /502.html;
  
  location ~ [^/]\.php(/|$) {
    #fastcgi_pass remote_php_ip:9000;
    fastcgi_pass unix:/dev/shm/php-cgi.sock;
    fastcgi_index index.php;
    include fastcgi.conf;
  }
  location ~ .*\.(gif|jpg|jpeg|png|bmp|swf|flv|mp4|ico)$ {
    expires 30d;
    access_log off;
  }
  location ~ .*\.(js|css)?$ {
    expires 7d;
    access_log off;
  }
  location ~ /(\.user\.ini|\.ht|\.git|\.svn|\.project|LICENSE|README\.md) {
    deny all;
  }
}

www.b.com配置

server {
  listen 80;
  listen [::]:80;
  server_name www.b.com;
  access_log off;
  index index.html index.htm index.php;
  root /data/wwwroot/www.b.com;
  
  include /usr/local/nginx/conf/rewrite/none.conf;
  #error_page 404 /404.html;
  #error_page 502 /502.html;
  
  location ~ [^/]\.php(/|$) {
    #fastcgi_pass remote_php_ip:9000;
    fastcgi_pass unix:/dev/shm/php-cgi.sock;
    fastcgi_index index.php;
    include fastcgi.conf;
  }
  location ~ .*\.(gif|jpg|jpeg|png|bmp|swf|flv|mp4|ico)$ {
    expires 30d;
    access_log off;
  }
  location ~ .*\.(js|css)?$ {
    expires 7d;
    access_log off;
  }
  location ~ /(\.user\.ini|\.ht|\.git|\.svn|\.project|LICENSE|README\.md) {
    deny all;
  }
}


部署工具

本次NGINX部署使用oneinstack部署,默认为域名虚拟主机模式。

学无止境,谦卑而行.

目录
相关文章
|
4月前
|
Java 应用服务中间件 Apache
简介Nginx,Tomcat和 Apache
简介Nginx,Tomcat和 Apache
简介Nginx,Tomcat和 Apache
|
10月前
|
搜索推荐 应用服务中间件 nginx
Nginx系列教程(05) - 虚拟主机配置
Nginx系列教程(05) - 虚拟主机配置
416 0
|
4月前
|
域名解析 Ubuntu 应用服务中间件
Nginx实现虚拟主机
Nginx实现虚拟主机
|
4月前
|
运维 应用服务中间件 Shell
Nginx安装与虚拟主机配置shell脚本
Nginx安装与虚拟主机配置shell脚本
46 0
|
负载均衡 Java 应用服务中间件
【Nginx】第一章 Nginx简介
【Nginx】第一章 Nginx简介
79 0
|
9月前
|
缓存 负载均衡 应用服务中间件
Linux-----nginx的简介,nginx搭载负载均衡以及nginx部署前后端分离项目
Linux-----nginx的简介,nginx搭载负载均衡以及nginx部署前后端分离项目
225 2
|
4月前
|
监控 应用服务中间件 nginx
Nginx 简介和安装(二)
Nginx 简介和安装
109 0
|
4月前
|
缓存 应用服务中间件 nginx
Nginx 简介和安装(一)
Nginx 简介和安装
188 0
|
9月前
|
负载均衡 前端开发 应用服务中间件
Nignx及负载均衡&动静分离->nginx简介,nginx搭载负载均衡提供前后分离后台接口数据上,nginx搭载负载均衡提供前后分离后台接口数据下,前端项目打包,前端项目Linux部署
Nignx及负载均衡&动静分离->nginx简介,nginx搭载负载均衡提供前后分离后台接口数据上,nginx搭载负载均衡提供前后分离后台接口数据下,前端项目打包,前端项目Linux部署
68 0
|
11月前
|
负载均衡 应用服务中间件 nginx
Nginx简介与Docker Compose部署指南
Nginx是一款高性能的开源Web服务器和反向代理服务器,以其卓越的性能、可伸缩性和灵活性而闻名。它在全球范围内广泛用于托管Web应用程序、负载均衡、反向代理和更多场景中。在本文中,我们将首先介绍Nginx的基本概念,然后演示如何使用Docker Compose轻松部署Nginx服务器。
626 0
Nginx简介与Docker Compose部署指南