今天分享一个nginx的vhost.conf文件推荐配置的方法,因为服务器配置在网上可以百度到很多,但是.conf文件一个比较好的写法却很少,所以在这里分享一个范例出来,复制过去改一改就可以用于大部分网站的配置了
server{
listen 80;
server_name lytit.com api.lytit.com;
root 你的项目目录/public(现在大部分框架都要定位到这个目录,比如ThinkPHP、laravel等); # 该项要修改为你准备存放相关网页的路径
location / {
index index.php index.html index.htm;
#如果请求既不是一个文件,也不是一个目录,则执行一下重写规则
if (!-e $request_filename)
{
#地址作为将参数rewrite到index.php上。给出两种写法
rewrite ^/(.*)$ /index.php?s=$1;
rewrite ^(.*)$ /index.php?s=/$1 last;
#若是子目录则使用下面这句,将subdir改成目录名称即可。
#rewrite ^/subdir/(.*)$ /subdir/index.php?s=$1;
}
}
#api接口的跳转,可自定义或者直接删掉
location /api/ {
index index.php index.html index.htm;
#如果请求既不是一个文件,也不是一个目录,则执行一下重写规则
if (!-e $request_filename)
{
#地址作为将参数rewrite到index.php上。
#rewrite ^/(.*)$ /index.php?s=$1;
#若是子目录则使用下面这句,将subdir改成目录名称即可。
rewrite ^/api/(.*)$ /api/index.php?s=$1;
}
}
#以下规则根据项目需要,不用就删掉
#文件上传
location ~* ^\/upload\/.+\.(html|php)$ {
return 404;
}
#插件
location ~* ^\/plugins\/.+\.(html|php)$ {
return 404;
}
#主题
location ~* ^\/themes\/.+\.(html|php)$ {
return 404;
}
#以下配置尽量不要随意更改,保持原样就好
#proxy the php scripts to php-fpm
location ~ \.php(.*)$ {
include fastcgi_params;
fastcgi_index index.php;
fastcgi_split_path_info ^((?U).+\.php)(/?.+)$;
##pathinfo支持start
#定义变量 $path_info ,用于存放pathinfo信息
set $path_info "";
#定义变量 $real_script_name,用于存放真实地址
set $real_script_name $fastcgi_script_name;
#如果地址与引号内的正则表达式匹配
if ($fastcgi_script_name ~ "^(.+?\.php)(/.+)$") {
#将文件地址赋值给变量 $real_script_name
set $real_script_name $1;
#将文件地址后的参数赋值给变量 $path_info
set $path_info $2;
}
#配置fastcgi的一些参数
fastcgi_param SCRIPT_FILENAME $document_root$real_script_name;
fastcgi_param SCRIPT_NAME $real_script_name;
fastcgi_param PATH_INFO $path_info;
fastcgi_param PATH_TRANSLATED $document_root$path_info;
###pathinfo支持end
fastcgi_intercept_errors on;
fastcgi_pass 127.0.0.1:9000;
# fastcgi_pass 如果是 sock形式,可能是下面的配置
# fastcgi_pass unix:/tmp/php-cgi.sock
}
location ~ .*\.(html|htm|gif|jpg|jpeg|bmp|png|ico|txt|js|css|woff|woff2|ttf|eot|svg)$ { # 允许post重定向 add_header "Access-Control-Allow-Origin" "*"; add_header "Timing-Allow-Origin" "*"; root /usr/local/html/app/web; expires 30d; }
}
0 条评论