Nginx 在/var/www中有多个项目的服务器?
我有一个服务器,我想用作个人服务器,我的所有项目都在Nginx 在/var/www中有多个项目的服务器?,nginx,virtualhost,Nginx,Virtualhost,我有一个服务器,我想用作个人服务器,我的所有项目都在/var/www下 我目前有两个文件夹,/var/www/html和/var/www/site 我希望能够通过以下URL访问这些文件夹(123.123.123.123是我的服务器IP): 123.123.123.123/html和123.123.123/site 这是我的默认虚拟主机文件: server { listen 80 default_server; listen [::]:80 default_server;
/var/www
下
我目前有两个文件夹,/var/www/html
和/var/www/site
我希望能够通过以下URL访问这些文件夹(123.123.123.123
是我的服务器IP):
123.123.123.123/html
和123.123.123/site
这是我的默认虚拟主机文件:
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找不到,所以很明显我做错了什么(是的,我重新启动了nginx)
请帮忙 您只需要一个服务器
块,因为/html
和/site
都位于同一个服务器
使用nginx-t
检查nginx
是否真的重新启动而没有出现任何错误
由于/site
使用复杂的目录方案,因此需要使用嵌套的位置
块来获得正确的路径
您的第一个项目似乎有一个简单的静态和PHP文件安排。您可以使用root/var/www代码>语句将以/html
开头的URI映射到html
文件夹。更多信息,请参阅
像这样的东西可能适合你:
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;
}
}
}
默认服务器不需要服务器名称
我有一个问题。如果我转到某个路径,比如123.123.123.123/site/search
,它返回404,但是索引(123.123.123.123/site
)工作正常。我应该怎么做才能修复此问题,使路由正常工作?如果有帮助,这里是.htaccess
文件:我在.htaccess
文件中看不到任何奇怪的东西。基本检查:清除浏览器缓存,使用nginx-t
和/或nginx-t
测试配置,并查看访问和错误日志以了解发生的情况。