Nginx 站点启用,站点可用: 无法在 Ubuntu 12.04中的配置文件之间创建软链接

我试图在/etc/nginx/中的站点启用目录和站点可用目录中包含服务器块的配置文件之间创建软链接。

我使用的命令是:

sudo ln -s sites-available/foo.conf sites-enabled/

然后执行:

ls -l

结果是:

lrwxrwxrwx 1 parallels parallels 27 Aug  6 20:44 immigrationinformation.conf -> immigrationinformation.conf

其中 immigrationinformation.conf -> immigrationinformation.conf部分有一个红色字体的木炭。

然后当我试图访问这个软链接时,我被告知它已经坏了。

当我在站点可用目录中创建软链接时,即。

sudo ln -s sites-available/foo.conf sites-available/foo_link.conf

但是,如果我将它移动到支持站点的目录,链接就会再次中断。

我可以通过文件管理器 GUI 创建软链接,但不能通过命令行。我也可以创建硬链接没有问题。

我怀疑这是一个权限问题,所以我试着将所有的权限都设置为777个目录和目录本身,并将所有者更改为 root 以外的其他权限,但仍然没有成功。

如果你能帮忙,我会很感激的,谢谢。

202624 次浏览

You need to start by understanding that the target of a symlink is a pathname. And it can be absolute or relative to the directory which contains the symlink

Assuming you have foo.conf in sites-available

Try

cd sites-enabled
sudo ln -s ../sites-available/foo.conf .
ls -l

Now you will have a symlink in sites-enabled called foo.conf which has a target ../sites-available/foo.conf

Just to be clear, the normal configuration for Apache is that the config files for potential sites live in sites-available and the symlinks for the enabled sites live in sites-enabled, pointing at targets in sites-available. That doesn't quite seem to be the case the way you describe your setup, but that is not your primary problem.

If you want a symlink to ALWAYS point at the same file, regardless of the where the symlink is located, then the target should be the full path.

ln -s /etc/apache2/sites-available/foo.conf mysimlink-whatever.conf

Here is (line 1 of) the output of my ls -l /etc/apache2/sites-enabled:

lrwxrwxrwx 1 root root  26 Jun 24 21:06 000-default -> ../sites-available/default

See how the target of the symlink is relative to the directory that contains the symlink (it starts with ".." meaning go up one directory).

Hardlinks are totally different because the target of a hardlink is not a directory entry but a filing system Inode.

My site configuration file is example.conf in sites-available folder So you can create a symbolic link as

ln -s /etc/nginx/sites-available/example.conf /etc/nginx/sites-enabled/

You can overwrite the existing file ../sites-enabled/myproject by forcing ln like this

sudo ln -sf ../sites-available/myproject ../sites-enabled/

then

sudo service nginx restart

To shorten manual typing of each conf file, you can try from sites-enabled folder:

ln -s ../sites-available/*.conf .
sudo systemctl restart nginx