Php Nginx位置是否覆盖Laravel 5.2路线?
我有一个nginx web服务器,在端口80上提供Laravel 5.2应用程序 通过访问mydomain.com,我的laravel应用程序启动了,一切都按预期运行 另一方面,我有一个运行在端口83上的nodejs应用程序,它提供其他类型的内容。 当我想通过主域上的反向代理服务我的nodejs内容时,问题就来了 我要做的是让nginx在domain.com/api/info/socket我的nodejs应用程序上提供服务,而不用laravel试图用它的路由系统解析url。以下是我尝试使用的nginx配置:Php Nginx位置是否覆盖Laravel 5.2路线?,php,node.js,laravel,nginx,server,Php,Node.js,Laravel,Nginx,Server,我有一个nginx web服务器,在端口80上提供Laravel 5.2应用程序 通过访问mydomain.com,我的laravel应用程序启动了,一切都按预期运行 另一方面,我有一个运行在端口83上的nodejs应用程序,它提供其他类型的内容。 当我想通过主域上的反向代理服务我的nodejs内容时,问题就来了 我要做的是让nginx在domain.com/api/info/socket我的nodejs应用程序上提供服务,而不用laravel试图用它的路由系统解析url。以下是我尝试使用的ng
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吗?试着将
位置/api/info/socket
块移到位置/
上面,一切都很好。您只需要添加几行
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;
}
另外,您需要将节点应用程序放入公共目录,不要忘记重新启动nginxsudo服务nginx restart
如果它解决了您的问题,请告诉我。:) 您有权为子域创建dns条目吗?可能会为您简化。这当然会有所帮助,但如果可能,我希望将所有公共端点保留在domain.com/api下。可以使用
api.domain.com
并在主域上转发请求。是不是proxy\u set\u header Host$http\u Host代码>?您是否正在收听localhost
或127.0.0.1
?此外,您可能需要使用proxy\u-pass$scheme://localhost:83;代码>这样您就不会将所有流量重定向到https@Blake我通过从http重定向到https来强制使用https,那么$scheme还需要吗?只是尝试了一下,我得到了完全相同的问题,Laravel的404页面。我还修改了location块,按照文档中的建议添加了=以进行精确匹配,但它也不起作用。Replacetry\u files$uri$uri//index.php?$query\u string使用try_files$uri$uri//index.php$is_args$args代码>不幸的是,这也不能解决问题:(感谢您尝试帮助我:)我做了这些更改,但仍然存在相同的问题。如果我发布整个默认配置,而不仅仅是相关的服务器块,会更好吗?我只是用其他信息更新了主要问题。请允许我指出/stream并没有被laravel解析,而是按预期工作,这真的很奇怪。我也在用sudo systemctl restart nginx重新启动,不知道它是否改变了什么。Ubuntu 16.04好的,告诉我你想要什么样的urlmydomain.com/api
或mydomain.com/api/info/socket
?你想访问的url是什么?现在我的Laravel应用程序解析了几个url,比如:mydomain.com/api/info/poll,所以我想在没有Laravel干扰的情况下访问的新url是mydomain.com/api/info/socket。现在我正在尝试访问该url,我从Laravel获得了默认的404。那么/api/info/socket
上的条目文件是什么index.html
还是别的什么?