1添加虛擬主機
進入 /usr/local/nginx/conf/vhost 目錄, 創建虛擬主機配置文件 demo.neoease.com.conf ({域名}.conf).
2. 打開配置文件, 添加服務如下:
server {
listen 80;
server_name demo.neoease.com;
index index.html index.htm index.php;
root /var/www/demo_neoease_com;
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;
}
3. 打開 Nginx 配置文件 /usr/local/nginx/conf/nginx.conf, 在 http
范圍引入虛擬主機配置文件如下:
include vhost/*.conf;
這句也可以直接包含虛擬主機文件的地址 例如: /usr/local/nginx/conf/vhost/demo.neoease.com.conf
4.重啟apache --nginx
service nginx restart
支持php
server {
listen 80;
server_name demo.neoease.com;
index index.html index.htm index.php;
root /var/www/demo_neoease_com;
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;
}
圖片防盜鏈
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 的虛擬主機配置修改如下:
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;
}
LNMP 套件在提供了 WordPress 為靜態配置文件 /usr/local/nginx/conf/wordpress.conf, 在虛擬主機配置的 server 范圍引用如下即可.
include wordpress.conf; |
如果你使用 LNMP 套件, 進入 WordPress 后台發現會出現 404 頁面, wp-admin 后面缺少了斜桿 /
, 請在 wordpress.conf 最后添加以下語句:
rewrite /wp-admin$ $scheme://$host$uri/ permanent; |