Nginx 虛擬主機 VirtualHost 配置


增長 Nginx 虛擬主機php

配置 Virtual host 步驟以下:html

1. 進入 nginx的安裝目錄,個人目錄是在/usr/local/nginx,找到nginx 的配置文件/usr/local/nginx/conf/nginx.conf並打開,在http{}範圍引入虛擬主機配置文件以下:nginx

include vhost/*.conf;

2. 在/usr/local/nginx/conf/vhost目錄下,建立對應虛擬主機的配置文件 www.demo.com.conf (文件格式{域名}.conf),每一個虛擬主機(域名)對應一個配置文件。打開配置文件, 添加服務以下:bash

server {
	listen       80;
	server_name www.demo.com;
     location / {
       root /var/www/demo;
       index index.html index.htm index.php;
     }
     //日誌保存方式以及存放位置
	log_format www.demo.com '$remote_addr - $remote_user [$time_local] $request'
	'$status $body_bytes_sent $http_referer '
	'$http_user_agent $http_x_forwarded_for';
	access_log  /var/log/www.demo.com.log www.demo.com;
     //支持php,這裏使用的是 FastCGI, 修改以下

     location ~ .*\.(php|php5)?$ {
        #網站目錄
        root /var/www/test;
        #phpcgi端口,默認9000
        fastcgi_pass 127.0.0.1:9000;
        fastcgi_index index.php;
        #document_root指向的就是網站目錄
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
}

}post

 

 

4. 重啓 Nginx 服務, 執行如下語句.網站

/etc/init.d/nginx reload
 

圖片防盜鏈

圖片做爲重要的耗流量大的靜態資源, 可能網站主並不但願其餘網站直接引用, Nginx 能夠經過 referer 來防止外站盜鏈圖片.google

server {
	listen       80;
	server_name demo.neoease.com;
	index index.html index.htm index.php;
	root  /var/www/demo_neoease_com;
 
	# 這裏爲圖片添加爲期 1 年的過時時間, 而且禁止 Google, 百度和本站以外的網站引用圖片
	location ~ .*\.(ico|jpg|jpeg|png|gif)$ {
		expires 1y;
		valid_referers none blocked demo.neoease.com *.google.com *.baidu.com;
		if ($invalid_referer) {
			return 404;
		}
	}
 
	log_format demo.neoease.com '$remote_addr - $remote_user [$time_local] $request'
	'$status $body_bytes_sent $http_referer '
	'$http_user_agent $http_x_forwarded_for';
	access_log  /var/log/demo.neoease.com.log demo.neoease.com;
}

WordPress 僞靜態配置

若是將 WordPress 的連接結構設定爲 /%postname%//%postname%.html 等格式時, 須要 rewrite URL, WordPress 提供 Apache 的 .htaccess 修改建議, 但沒告知 Nginx 該如何修改. 咱們能夠將 WordPress 的虛擬主機配置修改以下:spa

server {
	listen       80;
	server_name demo.neoease.com;
	index index.html index.htm index.php;
	root  /var/www/demo_neoease_com;
 
	location / {
		if (-f $request_filename/index.html){
			rewrite (.*) $1/index.html break;
		}
		if (-f $request_filename/index.php){
			rewrite (.*) $1/index.php;
		}
		if (!-f $request_filename){
			rewrite (.*) /index.php;
		}
	}
	rewrite /wp-admin$ $scheme://$host$uri/ permanent;
 
	location ~ .*\.(php|php5)?$ {
		fastcgi_pass unix:/tmp/php-cgi.sock;
		fastcgi_index index.php;
		include fcgi.conf;
	}
 
	log_format demo.neoease.com '$remote_addr - $remote_user [$time_local] $request'
	'$status $body_bytes_sent $http_referer '
	'$http_user_agent $http_x_forwarded_for';
	access_log  /var/log/demo.neoease.com.log demo.neoease.com;
}
相關文章
相關標籤/搜索