nginx 虚拟主机配置分析

符号 阅读:230 2021-03-31 21:36:13 评论:0
本文参考借鉴:http://blog.csdn.net/yuan_xw/article/details/51254674

1、虚拟主机管理:

1.1 nginx 管理虚拟主机:

虚拟主机使用的是特殊的软硬件技术,它把一台运行在互联网的服务器分成一台台“虚拟”主机,每台虚拟的主机都可以是一个独立的网站,可以具有独立的域名,具有完整的Intent服务器功能(www、FTP、Email),同一台主机上的虚拟机之间是完全独立的。从访问者的角度来看,每一台虚拟主机和一台独立主机完全一样。
利用虚拟主机,不用为每个要运行的网站提供一台单独服务器,虚拟主机提供了在同一台服务器,运行多个网站功能。

1.2 Nginx 基本配置:

1、Nginx 主要的配置文件nginx.conf.
		# 全局区   有一个工作子进程,一般设置为CPU数 * 核数 
		worker_processes  1;  
 
		events { 
				# 一般是配置nginx进程与连接的特性 
				# 如1个word能同时允许多少连接,一个子进程最大允许连接1024个连接 
				worker_connections  1024; 
		} 
 
		# 配置HTTP服务器配置段 
		http { 
 
				# 配置虚拟主机段 
					server { 
					 
						# 定位,把特殊的路径或文件再次定位。 
		        location  { 
		            
		        }  
		    } 
 
		    server { 
		   			... 
		    } 
		}
2、Nginx.conf 系统默认配置参数

#user  nobody; 
worker_processes  1; 
 
#error_log  logs/error.log; 
#error_log  logs/error.log  notice; 
#error_log  logs/error.log  info; 
 
#pid        logs/nginx.pid; 
 
 
events { 
    worker_connections  1024; 
} 
 
 
http { 
    include       mime.types; 
    default_type  application/octet-stream; 
 
    #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  logs/access.log  main; 
 
    sendfile        on; 
    #tcp_nopush     on; 
 
    #keepalive_timeout  0; 
    keepalive_timeout  65; 
 
    #gzip  on; 
 
    server { 
        listen       80; 
        server_name  localhost; 
 
        #charset koi8-r; 
 
        #access_log  logs/host.access.log  main; 
 
        location / { 
            root   html; 
            index  index.html index.htm; 
        } 
 
        #error_page  404              /404.html; 
 
        # redirect server error pages to the static page /50x.html 
        # 
        error_page   500 502 503 504  /50x.html; 
        location = /50x.html { 
            root   html; 
        } 
 
        # proxy the PHP scripts to Apache listening on 127.0.0.1:80 
        # 
        #location ~ \.php$ { 
        #    proxy_pass   http://127.0.0.1; 
        #} 
 
        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000 
        # 
        #location ~ \.php$ { 
        #    root           html; 
        #    fastcgi_pass   127.0.0.1:9000; 
        #    fastcgi_index  index.php; 
        #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name; 
        #    include        fastcgi_params; 
        #} 
 
        # deny access to .htaccess files, if Apache's document root 
        # concurs with nginx's one 
        # 
        #location ~ /\.ht { 
        #    deny  all; 
        #} 
    } 
 
 
    # another virtual host using mix of IP-, name-, and port-based configuration 
    # 
    #server { 
    #    listen       8000; 
    #    listen       somename:8080; 
    #    server_name  somename  alias  another.alias; 
 
    #    location / { 
    #        root   html; 
    #        index  index.html index.htm; 
    #    } 
    #} 
 
 
    # HTTPS server 
    # 
    #server { 
    #    listen       443 ssl; 
    #    server_name  localhost; 
 
    #    ssl_certificate      cert.pem; 
    #    ssl_certificate_key  cert.key; 
 
    #    ssl_session_cache    shared:SSL:1m; 
    #    ssl_session_timeout  5m; 
 
    #    ssl_ciphers  HIGH:!aNULL:!MD5; 
    #    ssl_prefer_server_ciphers  on; 
 
    #    location / { 
    #        root   html; 
    #        index  index.html index.htm; 
    #    } 
    #} 
 
} 

1.3、基于IP地址虚拟主机配置

1、编辑配置文件:D:\nginx\nginx-1.10.2\conf\nginx.conf
#user  nobody; 
worker_processes  1; 
 
#error_log  logs/error.log; 
#error_log  logs/error.log  notice; 
#error_log  logs/error.log  info; 
 
#pid        logs/nginx.pid; 
 
 
events { 
    worker_connections  1024; 
} 
 
 
http { 
    include       mime.types; 
    default_type  application/octet-stream; 
 
    #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  logs/access.log  main; 
 
    sendfile        on; 
    #tcp_nopush     on; 
 
    #keepalive_timeout  0; 
    keepalive_timeout  65; 
 
    #gzip  on; 
 
    server { 
        listen       80; 
        server_name  10.100.81.118; 
 
        #charset koi8-r; 
 
        #access_log  logs/host.access.log  main; 
 
        location / { 
            root   ip; 
            index  index.html index.htm; 
        } 
 
        #error_page  404              /404.html; 
 
        # redirect server error pages to the static page /50x.html 
        # 
        error_page   500 502 503 504  /50x.html; 
        location = /50x.html { 
            root   html; 
        } 
 
        # proxy the PHP scripts to Apache listening on 127.0.0.1:80 
        # 
        #location ~ \.php$ { 
        #    proxy_pass   http://127.0.0.1; 
        #} 
 
        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000 
        # 
        #location ~ \.php$ { 
        #    root           html; 
        #    fastcgi_pass   127.0.0.1:9000; 
        #    fastcgi_index  index.php; 
        #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name; 
        #    include        fastcgi_params; 
        #} 
 
        # deny access to .htaccess files, if Apache's document root 
        # concurs with nginx's one 
        # 
        #location ~ /\.ht { 
        #    deny  all; 
        #} 
    } 
 
 
    # another virtual host using mix of IP-, name-, and port-based configuration 
    # 
    #server { 
    #    listen       8000; 
    #    listen       somename:8080; 
    #    server_name  somename  alias  another.alias; 
 
    #    location / { 
    #        root   html; 
    #        index  index.html index.htm; 
    #    } 
    #} 
 
 
    # HTTPS server 
    # 
    #server { 
    #    listen       443 ssl; 
    #    server_name  localhost; 
 
    #    ssl_certificate      cert.pem; 
    #    ssl_certificate_key  cert.key; 
 
    #    ssl_session_cache    shared:SSL:1m; 
    #    ssl_session_timeout  5m; 
 
    #    ssl_ciphers  HIGH:!aNULL:!MD5; 
    #    ssl_prefer_server_ciphers  on; 
 
    #    location / { 
    #        root   html; 
    #        index  index.html index.htm; 
    #    } 
    #} 
 
} 
nginx虚拟主机配置ip 总结:
   server { 
      listen  80; 
      server_name  10.100.81.118; 
      location / { 
              root    ip; 
              index index.html; 
      } 
    }


2、新建ip/index.html

<html> 
 <head> 
 <meta http-equiv="Content-Type" content="text/html; charset=GBK" /> 
 </head> 
 <body> 
 <h2>基于IP地址虚拟主机配置-index</h2> 
 </body> 
 </html>
效果截图:

1.4 基于端口虚拟主机配置

1、编辑配置文件:D:\nginx\nginx-1.10.2\conf\nginx.conf
    server { 
        listen       8082; 
        server_name  10.100.81.118; 
 
        #charset koi8-r; 
 
        #access_log  logs/host.access.log  main; 
 
        location / { 
            root   ip; 
            index  port.html port.htm; 
        }

2、新建ip/port.html
<html> 
 <head> 
 <meta http-equiv="Content-Type" content="text/html; charset=GBK" /> 
 </head> 
 <body> 
 <h2>基于端口虚拟主机配置-port</h2> 
 </body> 
 </html>

3、效果截图


1.5 基于域名虚拟主机配置

1、在http 大括号中添加如下代码段:
    server {   
        #监听端口 80   
        listen 80;    
                                 
        #监听域名abc.com;   
        server_name abc.com; 
           
        location / {               
                # 相对路径,相对nginx根目录。   
            root    abc;   
             
            # 默认跳转到index.html页面   
            index index.html;                  
        }   
    }  

2、新建abc/index.html





声明

1.本站遵循行业规范,任何转载的稿件都会明确标注作者和来源;2.本站的原创文章,请转载时务必注明文章作者和来源,不尊重原创的行为我们将追究责任;3.作者投稿可能会经我们编辑修改或补充。

发表评论
搜索
KIKK导航

KIKK导航

排行榜
关注我们

一个IT知识分享的公众号