如何在nginx中设置虚拟主机?

如何在nginx中设置虚拟主机?,nginx,web,fastcgi,php,Nginx,Web,Fastcgi,Php,我正在使用以下配置设置,但它正在执行时下载索引源文件。但是,如果将index.html而不是index.php放入,它将显示index.html的输出 总的来说,它似乎不是在读取php文件 server { server_name test.com; root /var/www/test; index index.html index.php; # serve static files directly location ~* \.(jpg|jpeg|gif|css|png|j

我正在使用以下配置设置,但它正在执行时下载索引源文件。但是,如果将index.html而不是index.php放入,它将显示index.html的输出

总的来说,它似乎不是在读取php文件

 server {
 server_name test.com;

 root /var/www/test;

 index index.html index.php;

 # serve static files directly
 location ~* \.(jpg|jpeg|gif|css|png|js|ico|html)$ {
 access_log off;
 expires max;
}

location / {
    # Check if a file or directory index file exists, else route it to index.php.
    try_files $uri $uri/ /index.php;
}

location ~ /\.ht {
deny  all;
}
}

必须设置PHP-FPM或fastcgi,否则将获得普通文件:

fastcgi_pass 127.0.0.1:9000;
fastcgi_param SCRIPT_FILENAME /var/www/test/index.php;
fastcgi_param PATH_INFO $fastcgi_script_name;
fastcgi_pass_header Host;
fastcgi_pass_header X-Real-IP;
include /etc/nginx/fastcgi_params;

PHP-FPM应该在端口9000中运行

server {
    server_name example.com www.example.com;
    root /path/to/root;
    location / {
        try_files $uri $uri/ /index.php$request_uri;
    }
    location ~* \.php$ {
        include fastcgi_params;
        fastcgi_pass unix://var/run/php5-fpm.sock # if you use sock files
        fastcgi_pass http://127.0.0.1:9000; # if you use port 9000
    }
}
根据您的网站使用的内容,您需要对
fastcgi\u pass
行中的一行进行注释。当然,您需要自定义
try\u files
行,以匹配php获取路由的方式,例如,如果您使用
QUERY\u STRING
,您可以执行以下操作

try_files $uri $uri/ index.php?$request_uri; # mind the extra `?`

如果您遇到错误网关[502]错误,请再次检查
/var/run
目录中sock文件的路径

在设置fastcgi之前是否需要安装一些东西?我已经安装了nginx,这就足够了吗?您需要php fpm,作为服务运行。这将执行您的php文件。非常感谢。我现在可以执行php文件了。但它不接受任何其他url。我的意思是,在Apache中,我们需要重写mod_,同样,我们需要在这里做什么?例如
https://test.com/auth/register
无法识别,因此必须通过类似/var/www/test/$uri的方式更改/var/www/test/index.php