【发布时间】:2017-02-15 22:25:49
【问题描述】:
我有一个 nginx Web 服务器,在端口 80 上为 Laravel 5.2 应用程序提供服务。
通过访问 mydomain.com,我的 laravel 应用程序启动,一切都按预期工作。
另一方面,我有一个 nodejs 应用程序在端口 83 上运行,它提供其他类型的内容。 当我想通过主域上的反向代理提供我的 nodejs 内容时,问题就来了。
我要做的是让 nginx 在 domain.com/api/info/socket 我的 nodejs 应用程序上服务,而 laravel 不会尝试使用它的路由系统解析该 url。这是我尝试这样做的 nginx 配置:
server {
listen 80 default_server;
listen [::]:80 default_server;
#access_log /var/log/nginx/access_log combined;
#index index.php index.html index.htm index.nginx-debian.html
return 301 https://$server_name$request_uri;
}
server {
# SSL configuration
listen 443 ssl default_server;
listen [::]:443 ssl default_server;
include snippets/ssl-mydomain.com.conf;
include snippets/ssl-params.conf;
access_log /var/log/nginx/access_log combined;
index index.php index.html index.htm index.nginx-debian.html
server_name mydomain.com www.mydomain.com;
location / {
root /var/www/mydomain.com/public;
try_files $uri $uri/ /index.php?$query_string;
}
location /stream {
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_read_timeout 300;
proxy_pass http://localhost:9999/stream;
}
# This will not let laravel parse this url. replace index.html if you have some other entry point.
location /api/info/socket {
try_files $uri $uri/ /api/info/socket/index.html;
}
location = /api/info/socket {
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_read_timeout 300;
proxy_pass http://localhost:83;
}
location ~ \.php$ {
set $php_root /var/www/mydomain.com/public;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/run/php/php7.0-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $php_root$fastcgi_script_name;
include fastcgi_params;
}
location ~ /\.ht {
deny all;
}
location ~ /.well-known {
allow all;
}
}
但每次我访问该 URL 时,我都会收到 laravel 404 错误消息,因为它不是我的路由配置的一部分。关于如何在不让 Laravel 接管的情况下强制 nginx 提供特定 URL 的任何想法?
【问题讨论】:
-
您是否有权为子域创建 dns 条目?可以为您简化。
-
这当然有帮助,但如果可能的话,我想将所有公共端点保留在 domain.com/api 下
-
可以使用
api.domain.com转发主域上的请求。不是proxy_set_header Host $http_host;吗?你在收听localhost或127.0.0.1吗? -
另外,您可能需要使用
proxy_pass $scheme://localhost:83;,这样您就不会将所有流量重定向到 https 之外 -
@Blake 我通过从 http 重定向到 https 来强制执行 https,那么 $scheme 还需要吗?
标签: php node.js laravel nginx server