增长 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 / { //日志保存方式以及存放位置 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; }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 的连接结构设定为 /%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; } |