只需微调nginx的初始配置就可以,至于你用的 alias ,我这里没有用到。建议你这样配置。关键配置都写了中文注释。
nginx.conf
……
server
{
# 本地默认
listen 127.0.0.1:80;
server_name _;
index index.html index.htm index.php;
root /home/wwwroot/default; # 路径设置到nginx服务跟目录
include enable-php.conf;
……
access_log /home/wwwlogs/access.log;
}
include vhost/*.conf; # 导入每个项目的独立配置
vhost/demo.conf
server
{
listen 80;
#listen [::]:80 default_server ipv6only=on;
server_name demo.vm.com; # 设置虚拟域名访问不同的项目
index index.php index.htm index.html;
root /home/wwwroot/default/demo/public; # 设置到项目的入口文件所在目录
#error_page 404 /404.html;
# Deny access to PHP files in specific directory
#location ~ /(wp-content|uploads|wp-includes|images)/.*\.php$ { deny all; }
# laravel 这里是每个框架的url解析规则,不同框架稍有不同
location / {
try_files $uri $uri/ /index.php?$query_string;
}
# include enable-php-pathinfo.conf;
# 设置 nginx 转发到 php-fpm 或者也可以配置到另一个文件里,然后通过 include 引入
location ~ [^/]\.php(/|$)
{
try_files $uri =404;
fastcgi_pass unix:/tmp/php-cgi.sock;
fastcgi_index index.php;
include fastcgi.conf;
}
location /nginx_status
{
stub_status on;
access_log off;
}
location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
{
expires 30d;
}
location ~ .*\.(js|css)?$
{
expires 12h;
}
location ~ /.well-known {
allow all;
}
location ~ /\.
{
allow all;
}
access_log /home/wwwlogs/access.log;
}