我有一个 nginx Web 服务器,在端口 80 上为 Laravel 5.2 应用程序提供服务。
通过访问 mydomain.com,我的 laravel 应用程序启动,一切都按预期运行。
另一方面,我有一个在端口 83 上运行的 Nodejs 应用程序,该应用程序提供其他类型的内容。 当我想通过主域上的反向代理提供 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 错误消息,因为它不是我的路由配置的一部分。知道如何强制 nginx 提供特定的 url 而不让 Laravel 接管吗?
最佳答案
一切都很好。您只需要添加几行即可。
server_name domain.com www.domain.com;
index index.php index.html index.htm index.nginx-debian.html
location / {
root /var/www/domain.com/public;
try_files $uri $uri/ /index.php?$query_string;
}
# 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;
#}
# If above doesn't work try this.
location /api/info/socket/ {
try_files $uri $uri/ @api;
}
location @api {
rewrite /api/info/socket/ /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;
}
# This is the php processing needed for laravel
location ~ \.php$ {
#include snippets/fastcgi-php.conf;
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;
}
此外,您需要将 Node 应用程序放入公共(public)目录中,并且不要忘记重新启动 nginx sudo service nginx restart
让我知道它是否解决了您的问题。 :)
关于php - Nginx 位置覆盖 Laravel 5.2 路由?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39908459/