zoukankan      html  css  js  c++  java
  • 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.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;
        }
      }
    

    基于端口虚拟主机配置

    一台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部署,默认为域名虚拟主机模式。

    学无止境,谦卑而行.
  • 相关阅读:
    [文摘20080731]小破孩的婚姻
    Response.Redirect和Server.Transfer(Execute)的区别小论集锦
    学习FotoVision 进行C# colorMatrix 对图片的处理 : 亮度调整 抓屏 翻转 随鼠标画矩形
    [转]通过分区(Partition)提升MySQL性能
    [书目20080829]软件测试技术经典教程
    [转]c# + mysql + 事务处理(转载于 《C#数据库事务原理及实践》)
    遭遇 VS 的 无法调试引用的类库项目(DLL)问题(生成下面的模块时,启用了优化或没有调试信息)
    [转]C#动态生成文字图片
    命令行 SC命令 及通过sc config 更该windows服务的启动类型等
    [转]flash 与 js 通讯方法
  • 原文地址:https://www.cnblogs.com/wangyang0210/p/14391505.html
Copyright © 2011-2022 走看看