我有一个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错误消息,因为它不是我的路由配置的一部分。关于如何在不让Laravel接管的情况下强制nginx为特定网址提供服务的任何想法?
答案 0 :(得分:0)
尝试将location /api/info/socket
块移到location /
答案 1 :(得分:0)
一切都很好。你只需添加几行。
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
如果它解决了您的问题,请告诉我。 :)