我有一个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;
}
另外,你需要把你的节点应用放到公共目录下,不要忘记重启nginx sudo service nginx restart
让我知道它是否解决了你的问题。:)
尝试将location /api/info/socket
块移到location /
之上