一、基础配置
下面配置设计几个方面
1 http重定向到https
2 feixc.com重定向到www.feixc.com
3 配置ssl证书,证书申请超级简单acme证书申请,不用dns配置,自动续期,个人网站首选-CSDN博客
4 静态文件location配置
5 php文件解析配置
server {
listen 80;
server_name feixc.com www.feixc.com;
rewrite ^(.*)$ https://www.feixc.com$1 permanent;
}
server {
listen 443 ssl;
server_name feixc.com www.feixc.com;
ssl_certificate /usr/local/nginx/certs/www.feixc.com.pem;
ssl_certificate_key /usr/local/nginx/certs/www.feixc.com.key;
ssl_session_cache shared:SSL:1m;
ssl_session_timeout 5m;
ssl_ciphers HIGH:!aNULL:!MD5;
ssl_prefer_server_ciphers on;
client_max_body_size 20m;
client_body_buffer_size 128k;
gzip on;
gzip_buffers 32 4K;
gzip_comp_level 6;
gzip_min_length 100;
gzip_types application/javascript text/css text/xml application/font-woff;
gzip_disable "MSIE [1-6]\.";
if ( $host = 'feixc.com') {
rewrite ^/(.*)$ https://www.feixc.com/$1 permanent;
return 301;
}
location ^~ /.well-known/acme-challenge/ {
default_type "text/plain";
root /usr/local/www;
}
location ~* \.(jpg|jpeg|png|gif|ico|css|js|pdf|swf|woff2|woff|ttf|svg)$ {
root /usr/local/www/feixc;
index index.html index.php;
}
location ~ \.php$ {
include fastcgi_params;
fastcgi_pass 127.0.0.1:9400;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /www/feixc/$fastcgi_script_name;
}
}
二、配置路由解析
由于nginx不同于apache,不能像apache解析.htaccess,所以我的配置还是传统的nginx配置
# 防止直接访问敏感文件
location ~* \.(sql|log)$ {
deny all;
}
# URL重写规则
# 处理资源详情页 - 新格式: /资源类型/detail/id
location ~ ^/article/detail/([0-9]+)/?$ {
try_files $uri /blog.php?id=$1&type=article;
}
location ~ ^/(code|plugin|software|tutorial|sucai)/detail/([0-9]+)/?$ {
try_files $uri /resource.php?id=$2&type=$1;
}
# 处理资源类型路由 - 统一处理所有资源类型的复杂路由
# 支持格式: /资源类型/category-分类id-属性别名-属性值-属性别名-属性值-page-页码
location ~ ^/(code|plugin|software|tutorial|sucai|article)/?(.*)$ {
try_files $uri /resources.php?type=$1&route_path=$2;
}
location ~ ^/website/?(.*)$ {
try_files $uri /websites.php?type=website&route_path=$1;
}
# 处理搜索页面
location = /search {
try_files $uri /search.php;
}
# 处理搜索路由 - 格式: /search/keyword-关键词-page-页码
location ~ ^/search/keyword-(.+?)-page-([0-9]+)/?$ {
try_files $uri /search.php?q=$1&page=$2;
}
# 处理搜索路由 - 格式: /search/keyword-关键词
location ~ ^/search/keyword-(.+)/?$ {
try_files $uri /search.php?q=$1;
}
# 处理旧格式的资源详情页(保持兼容性)
location ~ ^/resource/([0-9]+)/?$ {
try_files $uri /resource.php?id=$1;
}
# 处理用户相关页面
location ~ ^/user/(dashboard|profile)/?$ {
try_files $uri /user/$1.php;
}
# 处理认证相关页面
location ~ ^/(login|register|logout)/?$ {
try_files $uri /auth/$1.php;
}
# 处理上传页面
location = /upload {
try_files $uri /upload.php;
}
location = /admin {
try_files $uri /admin/index.php;
}