我有一个nginx web服务器,服务端口80上的Laravel 5.2应用程序。Nginx位置重写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;
}
}
但每次我访问该网址我得到的laravel 404错误消息,因为这不是我的路线配置的一部分。有关如何强制nginx在不让Laravel接管的情况下提供特定网址的想法?
您有权限为子域创建DNS条目吗?可以为你简化它。 – Blake
这当然可以帮助,但如果可能的话,我想将所有公共端点保留在domain.com/api下 – Kanadeko
可以使用'api.domain.com'并转发主域上的请求。是不是'proxy_set_header Host $ http_host;'?你在'localhost'或'127.0.0.1'上听吗? – Blake