/var/www 中有多个项目的服务器?
Server with multiple projects in /var/www?
我有一个服务器,我想用作个人服务器,我的所有项目都在 /var/www
下。
我目前有两个文件夹,/var/www/html
和 /var/www/site
。
我希望能够通过以下 URL 访问这些文件夹(123.123.123.123
是我的服务器 IP):
123.123.123.123/html
和 123.123.123.123/site
这是我的 default
虚拟主机文件:
server {
listen 80 default_server;
listen [::]:80 default_server;
root /var/www/html;
index index.php index.html index.htm index.nginx-debian.html;
server_name 123.123.123.123;
location / {
try_files $uri $uri/ =404;
}
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/run/php/php7.0-fpm.sock;
}
location ~ /\.ht {
deny all;
}
}
这是我为 /var/www/site
创建的,名为 site
:
server {
listen 80;
listen [::]:80;
# Because this site uses Laravel, it needs to point to /public
root /var/www/site/public;
index index.php index.html index.htm index.nginx-debian.html;
server_name 123.123.123.123;
location /site {
try_files $uri $uri/ /index.php?$query_string;
}
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/run/php/php7.0-fpm.sock;
}
location ~ /\.ht {
deny all;
}
}
但是当我转到 123.123.123.123/site
时,它显示 404 Not Found,很明显我做错了什么(是的,我重新启动了 nginx)。
请帮忙!
你只需要一个 server
块,因为 /html
和 /site
都住在同一个 server
.
使用 nginx -t
检查 nginx
是否确实重新启动而没有给出任何错误。
由于 /site
使用复杂的目录方案,您将需要使用嵌套的 location
块来获得正确的路径。
您的第一个项目似乎有一个简单的静态和 PHP 文件排列。您可以使用 root /var/www;
语句将以 /html
开头的 URI 映射到 html
文件夹。有关更多信息,请参阅 this document。
类似这样的东西可能对你有用:
server {
listen 80 default_server;
listen [::]:80 default_server;
root /var/www/;
index index.php index.html index.htm index.nginx-debian.html;
location / {
try_files $uri $uri/ =404;
}
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/run/php/php7.0-fpm.sock;
}
location ~ /\.ht {
deny all;
}
location ^~ /site {
alias /var/www/site/public;
if (!-e $request_filename) { rewrite ^ /site/index.php last; }
location ~ \.php$ {
if (!-f $request_filename) { return 404; }
include snippets/fastcgi-php.conf;
fastcgi_param SCRIPT_FILENAME $request_filename;
fastcgi_pass unix:/run/php/php7.0-fpm.sock;
}
}
}
默认服务器不需要server_name
。
我有一个服务器,我想用作个人服务器,我的所有项目都在 /var/www
下。
我目前有两个文件夹,/var/www/html
和 /var/www/site
。
我希望能够通过以下 URL 访问这些文件夹(123.123.123.123
是我的服务器 IP):
123.123.123.123/html
和 123.123.123.123/site
这是我的 default
虚拟主机文件:
server {
listen 80 default_server;
listen [::]:80 default_server;
root /var/www/html;
index index.php index.html index.htm index.nginx-debian.html;
server_name 123.123.123.123;
location / {
try_files $uri $uri/ =404;
}
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/run/php/php7.0-fpm.sock;
}
location ~ /\.ht {
deny all;
}
}
这是我为 /var/www/site
创建的,名为 site
:
server {
listen 80;
listen [::]:80;
# Because this site uses Laravel, it needs to point to /public
root /var/www/site/public;
index index.php index.html index.htm index.nginx-debian.html;
server_name 123.123.123.123;
location /site {
try_files $uri $uri/ /index.php?$query_string;
}
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/run/php/php7.0-fpm.sock;
}
location ~ /\.ht {
deny all;
}
}
但是当我转到 123.123.123.123/site
时,它显示 404 Not Found,很明显我做错了什么(是的,我重新启动了 nginx)。
请帮忙!
你只需要一个 server
块,因为 /html
和 /site
都住在同一个 server
.
使用 nginx -t
检查 nginx
是否确实重新启动而没有给出任何错误。
由于 /site
使用复杂的目录方案,您将需要使用嵌套的 location
块来获得正确的路径。
您的第一个项目似乎有一个简单的静态和 PHP 文件排列。您可以使用 root /var/www;
语句将以 /html
开头的 URI 映射到 html
文件夹。有关更多信息,请参阅 this document。
类似这样的东西可能对你有用:
server {
listen 80 default_server;
listen [::]:80 default_server;
root /var/www/;
index index.php index.html index.htm index.nginx-debian.html;
location / {
try_files $uri $uri/ =404;
}
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/run/php/php7.0-fpm.sock;
}
location ~ /\.ht {
deny all;
}
location ^~ /site {
alias /var/www/site/public;
if (!-e $request_filename) { rewrite ^ /site/index.php last; }
location ~ \.php$ {
if (!-f $request_filename) { return 404; }
include snippets/fastcgi-php.conf;
fastcgi_param SCRIPT_FILENAME $request_filename;
fastcgi_pass unix:/run/php/php7.0-fpm.sock;
}
}
}
默认服务器不需要server_name
。