Nginx 虚拟主机不工作 [英] Nginx virtual host not working
问题描述
我在本地主机上托管了一个 PHP 系统,我在 /var/www/html
中创建了 index.php
,如下所示:
I'm hosting a PHP system on my localhost, I created index.php
in the /var/www/html
like this:
// /var/www/html/index.php
<?php
phpinfo();
当我访问 localhost:80
时,它会显示我的 PHP 配置信息.
And when I access localhost:80
it's showing up my PHP configurations info.
但是当我将 /etc/nginx/sites-available/default
克隆到 /etc/nginx/sites-available/superstore
并更改了 server_name
它在sudo service nginx restart
后不起作用,即使sudo service nginx status
也返回成功.
But when I cloned /etc/nginx/sites-available/default
to /etc/nginx/sites-available/superstore
and changed server_name
it's not working after sudo service nginx restart
, even sudo service nginx status
is returning success.
我的 nginx 配置(/etc/nginx/sites-available/superstore
)是这样的:
My nginx config (/etc/nginx/sites-available/superstore
) is like this:
server {
listen 80;
root /var/www/html;
index index.php index.html index.htm index.nginx-debian.html;
server_name superstore.dev;
location / {
try_files $uri $uri/ =404;
}
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/run/php/php7.0-fpm.sock;
}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
location ~ /\.ht {
deny all;
}
}
我的 PHP 版本:
重启后的Nginx状态:
Nginx status after restart:
我将这个虚拟主机添加到我的 /etc/hosts
中,如下所示:
I added this virtual host to my /etc/hosts
like this:
127.0.0.1 superstore.dev
但是当我尝试访问 superstore.dev
时,它向我返回了这个错误:
But when I try to access superstore.dev
it's returning this error to me:
我在 Ubuntu 16 上通过以下命令安装了 PHP 和 Nginx:
I installed PHP and Nginx by following command on Ubuntu 16:
sudo apt install nginx php7.0-fpm -y
而且我确定我已经将 superstore
配置文件符号链接到 sites-enabled
文件夹.
And I'm sure that I already symbolic linked superstore
config file to sites-enabled
folder.
为什么我的虚拟主机不工作?
Why my virtual host is not working?
推荐答案
类似于 Apache 配置方案,文件夹 sites-available
应该包含您所有的配置文件,即使是您没有的配置文件想跑.
Similar to Apache config scheme, folder sites-available
is supposed to contain all your config files, even the ones you don't want to run.
还有第二个文件夹 sites-enabled
,其中包含指向要运行的配置文件的符号链接.
There is a second folder, sites-enabled
, that contains symlinks to the config files you want to run.
要创建符号链接,请运行 ln -s/etc/nginx/sites-available/superstore/etc/nginx/sites-enabled/
然后 service nginx reload
To create the symlink run ln -s /etc/nginx/sites-available/superstore /etc/nginx/sites-enabled/
and then service nginx reload
有关详细信息,请参阅答案 https://serverfault.com/a/424456/240702
For more info see answer https://serverfault.com/a/424456/240702
这篇关于Nginx 虚拟主机不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!