nginx - 在不同端点和相同域上提供后端和前端服务
nginx - serving backend and frontend on different endpoints and same domain
我已经在 OS X 上安装了 nginx php7.1, mysql 等等......基本测试示例正在运行。
当我尝试配置 nginx 以在 user.domain.dev/api/...
和 Angular 前端[=39] 上提供 Laravel 后端 =] 在 user.domain.dev/...
我得到 404 或 403。 Nginx错误日志大多是
/Users/name/Projects/domain.dev/api.domain.dev/" is forbidden
或
/Users/name/Projects/domain.dev/frontend.domain.dev/build/index.php" failed (2: No such file or directory)
我似乎无法理解 nginx 的位置指令,因此它们指向了错误的目录。感谢您的任何建议或指导。
我的配置是:
nginx.conf
user name staff;
worker_processes auto;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
ssl_certificate ssl/nginx.crt;
ssl_certificate_key ssl/nginx.key;
access_log /usr/local/var/log/nginx/access.log;
error_log /usr/local/var/log/nginx/error.log;
sendfile on;
keepalive_timeout 5;
gzip on;
include servers/*.conf;
}
servers/domain.dev.conf
server {
listen 80;
listen 443 ssl http2 default_server;
server_name domain.dev *.domain.dev www.domain.dev;
charset utf-8;
# removes trailing slashes (prevents SEO duplicate content issues)
if (!-d $request_filename)
{
rewrite ^/(.+)/$ / permanent;
}
# enforce NO www
if ($host ~* ^www\.(.*))
{
set $host_without_www ;
rewrite ^/(.*)$ $scheme://$host_without_www/ permanent;
}
##
## Backend HTTP server
##
location /api {
root /Users/name/Projects/domain.dev/api.domain.dev;
index index.php;
try_files $uri $uri/ /index.php?$query_string;
location ~ \.php$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
try_files $fastcgi_script_name =404;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi.conf;
}
}
##
## Frontend HTTP server
##
location ~* \.(jpg|jpeg|gif|css|png|js|ico|html)$ {
access_log off;
expires max;
}
location = /favicon.ico {
access_log off;
log_not_found off;
}
location = /robots.txt {
access_log off;
log_not_found off;
}
location / {
root /Users/name/Projects/domain.dev/frontend.domain.dev/build;
index index.html;
}
location ~ /\. {
deny all;
}
}
主要问题是 root
指令的使用不一致。您有两个文档根目录,每个应用程序一个,但只在您的两个位置指定。 server { ... }
块级别没有指定 root
,因此 if (!-d $request_filename)
没有意义,location ~* \.(jpg|jpeg|gif|css|png|js|ico|html)$
将始终导致 404
响应,等等
阅读 this document 了解 nginx
如何处理请求。
但是您可能应该在 server
块中设置前端根并在 location /api
块中用后端根覆盖它。
server {
root /Users/name/Projects/domain.dev/frontend.domain.dev/build;
location / { ... }
location ~* \.(jpg|jpeg|gif|css|png|js|ico|html)$ { ... }
location ^~ /api {
root /Users/name/Projects/domain.dev/api.domain.dev;
...
}
}
这会将后端文档根目录放置在:/Users/name/Projects/domain.dev/api.domain.dev/api
-- 请注意,URI 始终附加到 root
。
另请注意,^~
修饰符用于使前缀位置优先于同一级别的正则表达式位置。有关详细信息,请参阅 this document。
我不喜欢你设计中裸露的 if
方块。 if (!-d $request_filename)
可能应该用 try_files
directive, and if ($host ~* ^www\.(.*))
should probably be replaced by using a separate server
block. See this document 代替。
您的 location /api
块中的 try_files
语句包含不正确的默认 URI,它可能应该是 /api/index.php?$query_string
.
我已经在 OS X 上安装了 nginx php7.1, mysql 等等......基本测试示例正在运行。
当我尝试配置 nginx 以在 user.domain.dev/api/...
和 Angular 前端[=39] 上提供 Laravel 后端 =] 在 user.domain.dev/...
我得到 404 或 403。 Nginx错误日志大多是
/Users/name/Projects/domain.dev/api.domain.dev/" is forbidden
或
/Users/name/Projects/domain.dev/frontend.domain.dev/build/index.php" failed (2: No such file or directory)
我似乎无法理解 nginx 的位置指令,因此它们指向了错误的目录。感谢您的任何建议或指导。
我的配置是:
nginx.conf
user name staff;
worker_processes auto;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
ssl_certificate ssl/nginx.crt;
ssl_certificate_key ssl/nginx.key;
access_log /usr/local/var/log/nginx/access.log;
error_log /usr/local/var/log/nginx/error.log;
sendfile on;
keepalive_timeout 5;
gzip on;
include servers/*.conf;
}
servers/domain.dev.conf
server {
listen 80;
listen 443 ssl http2 default_server;
server_name domain.dev *.domain.dev www.domain.dev;
charset utf-8;
# removes trailing slashes (prevents SEO duplicate content issues)
if (!-d $request_filename)
{
rewrite ^/(.+)/$ / permanent;
}
# enforce NO www
if ($host ~* ^www\.(.*))
{
set $host_without_www ;
rewrite ^/(.*)$ $scheme://$host_without_www/ permanent;
}
##
## Backend HTTP server
##
location /api {
root /Users/name/Projects/domain.dev/api.domain.dev;
index index.php;
try_files $uri $uri/ /index.php?$query_string;
location ~ \.php$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
try_files $fastcgi_script_name =404;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi.conf;
}
}
##
## Frontend HTTP server
##
location ~* \.(jpg|jpeg|gif|css|png|js|ico|html)$ {
access_log off;
expires max;
}
location = /favicon.ico {
access_log off;
log_not_found off;
}
location = /robots.txt {
access_log off;
log_not_found off;
}
location / {
root /Users/name/Projects/domain.dev/frontend.domain.dev/build;
index index.html;
}
location ~ /\. {
deny all;
}
}
主要问题是 root
指令的使用不一致。您有两个文档根目录,每个应用程序一个,但只在您的两个位置指定。 server { ... }
块级别没有指定 root
,因此 if (!-d $request_filename)
没有意义,location ~* \.(jpg|jpeg|gif|css|png|js|ico|html)$
将始终导致 404
响应,等等
阅读 this document 了解 nginx
如何处理请求。
但是您可能应该在 server
块中设置前端根并在 location /api
块中用后端根覆盖它。
server {
root /Users/name/Projects/domain.dev/frontend.domain.dev/build;
location / { ... }
location ~* \.(jpg|jpeg|gif|css|png|js|ico|html)$ { ... }
location ^~ /api {
root /Users/name/Projects/domain.dev/api.domain.dev;
...
}
}
这会将后端文档根目录放置在:/Users/name/Projects/domain.dev/api.domain.dev/api
-- 请注意,URI 始终附加到 root
。
另请注意,^~
修饰符用于使前缀位置优先于同一级别的正则表达式位置。有关详细信息,请参阅 this document。
我不喜欢你设计中裸露的 if
方块。 if (!-d $request_filename)
可能应该用 try_files
directive, and if ($host ~* ^www\.(.*))
should probably be replaced by using a separate server
block. See this document 代替。
您的 location /api
块中的 try_files
语句包含不正确的默认 URI,它可能应该是 /api/index.php?$query_string
.