#禁止空主機(jī)頭訪問(wèn)
server {
listen 80 default_server;
server_name _;
return 500;
}
server {
listen 80;
server_name youdomain.com www.yourdomain.com;
#下面這條規(guī)定站點(diǎn)根目錄,再往下的配置就以此為根
root /usr/share/nginx/siterootpath;
index index.php;
#charset koi8-r;
#下面這句是訪問(wèn)log,按需開(kāi)關(guān)
#access_log /var/log/nginx/log/host.access.log main;
#favicon,站點(diǎn)的圖標(biāo),可以顯示在瀏覽器標(biāo)題欄上,和robots.txt兩個(gè)都不需要log
location = /favicon.ico {
log_not_found off;
access_log off;
}
location =/robots.txt {
allow all;
log_not_found off;
access_log off;
}
# 所有靜態(tài)文件都直通,解決了之前jpg等文件單獨(dú)設(shè)置直通的問(wèn)題,另外還可以直接支持固定鏈接
location / {
try_files $uri $uri/ /index.php?$args;
}
#自定義404
#error_page 404 /404.html;
#自定義錯(cuò)誤頁(yè)
# redirect server error pages to the static page /50x.html
#
location = /50x.html {
root /usr/share/nginx/html;
}
#對(duì)上傳目錄禁止執(zhí)行php,并阻止除圖片以外的任何文件類型
location /wp-content/uploads/ {
if ($request_filename !~* \.(jpg|jpeg|gif|png)$) {
return 403;
}
location ~ .*\.(php)?$ {
deny all;
}
}
# php文件發(fā)送到php5-fpm,現(xiàn)在這一段不需要寫root目錄了
#
location ~ \.php$ {
#檢查php文件是否真實(shí)存在,防止偽裝成jpg的php等
if (!-f $document_root$fastcgi_script_name) {
return 404;
}
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$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;
}
#js css 圖片等文件最大化有效期,不記錄訪問(wèn)
location ~* \.(js|css|png|jpg|jpeg|gif|ico)$ {
expires max;
log_not_found off;
}
}