Nginx额外篇之虚拟主机

Nginx中的server_name指令主要用于配置基于名称的虚拟主机。

组成结构:

server {
     定义基本虚拟机信息(访问端口(listen 80;地址(server_name),根目录(root))
     配置指令(跨域,缓存,错误页面)
     location / {                #localtion指令可以重复多个的
     	#处理请求 #这里还有指令,比如目录路径
     }    
     ....
}

示例1:

server {                                       
    listen  80;                                      
    server_name ab.text.com ac.text.com; #指定域名(也可以是ip)多个域名用空格隔开
    index index.html index.htm index.php; #访问页面的先后顺序(优先访问的房前面)[可选]
    root  /data/pic/abc;  #网站的根目录
    
    access_log  logs/pic.abc.com ytlog;
    
    #提供php的支持
    location ~ \.php {           
    	fastcgi_pass       unix:/tmp/php-cgi.sock;
   		fastcgi_index      index.php;
    	fastcgi_param      SCRIPT_FILENAME  $document_root;$fastcgi_script_name;    
     	include            fastcgi_params;
    
	location ~ \.php$ {
        proxy_cache cache_text;
        proxy_cache_valid 200 1d;#此处控制的是nginx服务器缓存时间,过期后去源服务器重新取数据。
        proxy_cache_path /data/nginx_cache/ #缓存文件存放路劲
        proxy_hide_header "Set-Cookie";	#不显显示的header信息
        proxy_ignore_headers "Cache-Control" "Set-Cookie" "Expires"; #忽略哪些信息
        add_header Nginx-Cache "$upstream_cache_status";
        include proxy.conf; 
        proxy_pass http://text_server;
        expires 1d;
       }
	 } 

示例2

    server
    	listen  443 ssl;                                      
    	server_name *.text.com ;         	 #指定域名(也可以是ip)多个域名用空格隔开
    	index index.html index.htm index.php;            #访问页面的先后顺序(优先访问的房前面)[可选]
   		root  /data/pic/abc;     
        
        keepalive_timeout 70;		##设置keep-aplive(长连接)超时时间
        ssl_certificate /opt/app/nginx/conf/cert/ngxdefault.crt;	#添加server.pem访问路劲(第一个证书文件路劲)		
        ssl_certificate_key /opt/app/nginx/conf/cert/ngxdefault.key;	#添加server.key访问路劲(第二个证书文件访问路劲)

        ssl_ciphers ".......";			#指定加密算法
        ssl_protocols TLSv1.1 TLSv1.2;  #启用指定协议(使用哪种加密协议支持ssl版本)
        ssl_prefer_server_ciphers on;  #指定在使用SSLv3和TLS协议时,服务器密码应优先于客户端密码。)
        ssl_session_cache shared:SSL:10m; #设置ssl会话缓存大小,有4个值
        			设置存储会话参数的高速缓存的类型和大小。缓存可以是以下任何一种类型:
					off
					严禁使用会话缓存:nginx明确告诉客户端会话可能不会被重用。
					none
					会话缓存的使用被轻轻地禁止:nginx告诉客户端会话可能被重用,但实际上不会将会话参数存储在缓存中。
					builtin
					建立在OpenSSL中的缓存; 仅由一个工作进程使用。缓存大小在会话中指定。如果没有给出大小,则等于20480个会话。内置缓存的使用可能导致内存碎片。
					shared
					所有工作进程之间共享的缓存。缓存大小以字节为单位指定; 一兆字节可以存储大约4000个会话。每个共享缓存都应该有一个任意的名字。具有相同名称的缓存可以在多个虚拟服务器中使用。
					两种缓存类型都可以同时使用,例如:
					ssl_session_cache builtin:1000 shared:SSL:10m;
					注意:但只使用没有内置缓存的共享缓存应该更有效率。
							
	    ssi on;	#服务器嵌套,主要是实现网站的内容更新,时间和日期的动态显示,以及执行shell和CGI脚本程序等复杂的功能。
    	ssi_silent_errors on; #默认是off,开启后在处理SSI文件出错时不输出错误提示:”[an error occurred while processing the directive] ”
    	#ssi_types #默认是支持ssi_types text/html 如果需要shtml支持,则需要设置:ssi_types text/shtml
    	#适用于http,server,location等模块 
    	
    	fastcgi_intercept_errors on; #是否将4xx和5xx错误信息到客户端,或允许nginx使用error_page处理错误信息。
    	error_page  404 /404.html;
    	location = /50x.html {
        	root   html;
        }
   }

示例3

server {
    listen 80;
    server_name a2.test.com;
    index index.shtml index.html index.htm index.php;

    location / {
        #include proxy.conf;
        add_header 'Access-Control-Allow-Origin' *;
        proxy_pass http://192.168.1.20;
        
        proxy_connect_timeout 300s;
        proxy_send_timeout 900;
        proxy_read_timeout 900;
        proxy_buffer_size 256k;
        proxy_buffers 4 256k;
        proxy_busy_buffers_size 256k;
        proxy_temp_file_write_size 256k;
        proxy_max_temp_file_size 1024m;
        
       #支持对代理服务器请求头进行定义,默认只定义了(proxy_set_header Host $proxy_host; proxy_set_header Connection close;这2个字段)想要在客户端获取更多header信息可以通过此段定义
        proxy_set_header Accept-Encoding '';
        proxy_set_header Referer $http_referer;
        proxy_set_header Cookie $http_cookie;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

        proxy_hide_header Vary; #隐藏某个head头部信息,通过此指令可以将不显示给用户的信息隐藏。语法:proxy_hide_header felied;
        proxy_next_upstream http_500 http_502 http_503 http_504 http_404 error timeout invalid_header;  #一种容错机制,在当前代理出现后面这些错误就会将请求传递给下一个服务器,为off就是关闭此功能。 
        proxy_redirect default;   #为off的时候取消所proxy_redirect对当前的影响
        proxy_ignore_client_abort on; #为on的时候忽略499错误
        proxy_intercept_errors on; #获得后端服务器响应后,可以根据响应状态码的值进行拦截错误处理,与error_page 指令相互结合。用在访问上游服务器出现错误的情况下。
    }

    access_log /data/logs/www/a2.test.com-proxy.log jflog;
}
#这些代理字段可以根据需求自己添加或者删除 官方文档:http://nginx.org/en/docs/http/ngx_http_proxy_module.html#proxy_ignore_client_abort

示例4

server {
    listen       80;
    listen       443 ssl;
    server_name  ab.text1.com;
    root /data/ab;
    index index.php;

    keepalive_timeout 70;
        ssl_certificate       /usr/local/nginx/cert/allab.crt;
        ssl_certificate_key   /usr/local/nginx/cert/allab.key;

        ssl_ciphers "EECDH+AESGCM:EDH+AESGCM:AES256+EECDH:AES256+EDH";
        ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
        ssl_prefer_server_ciphers on;
        ssl_session_cache shared:SSL:10m;
      
    location = /favicon.ico {
        log_not_found off;
        access_log off;
    }
    location / {
        if (!-e $request_filename) {
            rewrite  ^(.*)$  /index.php?s=$1  last;
            break;
        }
    }

    location ~ ^/index\.php$ {
        fastcgi_pass unix__tmp_php5_cgi_sock;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
        include        fastcgi_params;
    }

    location ~ \.(php|git) {
        return 404;
    }

	#错误页面优雅显示(error_page 通过此参数就可以实现,一般Nginx直接返回的错误页面都是比较丑的,用户可以通过这个参数定义不错的错误页面)
    error_page 404 /404.html;
    error_page 502 503 504 /50x.html;
    error_page 500 501 502 503 504 http://ac.test2.com/error2.jpg;
    #有几种方法任选
    access_log   /data/logs/www/ab_https.log jflog;
}

示例5

server {
    listen 80;
    server_name *.text.com;
    index index.php;
    root /data/www/text;
    
    location / {
        root /data/www/text;
        index index.php;

        add_header Access-Control-Allow-Origin *;
        add_header Access-Control-Allow-Headers X-Requested-With,Content-Type;
        add_header Access-Control-Allow-Methods GET,POST,OPTIONS;
        add_header Access-Control-Allow-Headers AUTH;

        fastcgi_pass unix__tmp_php_cgi_sock;
        try_files $uri =404;  #当用户访问此资源的时候,如果uri不存在则把404发送给后端,调取后端写好的404页面,
        include fastcgi_params;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_param SCRIPT_NAME $fastcgi_script_name;
    }
}

	loaction / {
	try_files $uri @404
	}
	loaction @a404{
	proxy_pass http://127.0.0.1:404.html
	include proxy.conf
	}

#多种虚拟主机方式(上面的是基于域名的虚拟主机)
server {                                        # 基于IP地址的虚拟主机
    listen 80;
    server_name 192.168.100.25;
    location / {
            root /www/longshuai/;
            index index.html index.htm;

    server {                                #基于端口的虚拟主机
    listen 8080;
    server_name 192.168.100.25;
    location / {
            root /www/xiaofang/;
            index index.html index.htm;
    }
    同时server_name可以定义多个主机名,但是第一个为虚拟主机的首要主机名,例如
    server_name *.abc.com  m.abc.com  www.abc.*                        #同时看*位置的不同,表示可以匹配起始部分或者结尾部
   
     try_files $uri =404;                                        #主要用于重定向(网上说的是可以替代rewrite重写功能)
     fastcgi_pass             unix__tmp_php_cgi_sock;                               
     fastcgi_param             SCRIPT_FILENAME $document_root$fastcgi_script_name;  
     fastcgi_param             SCRIPT_NAME $fastcgi_script_name;       
     include fastcgi_params;          
 }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值