php - Nginx 位置覆盖 Laravel 5.2 路由?

标签 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。这是我尝试执行此操作的 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/

相关文章:

php - 使用 Nginx 设置 Laravel

node.js - NPM 漏洞 - 需要人工审核

node.js - Node js 中的静态类方法或纯函数

node.js - Jest的spyOn方法不会被调用,但是一旦改变测试用例顺序就会被调用

PHP/Laravel - 更新行中的 SQL 整数列将值加 1

php - 合并 Mysql 列输出作为 php 中的逗号分隔

刷新表单时用户选择单选按钮的 php 停留(由于错误检查或其他)

php - PHP中大括号之间的匹配文本

laravel - phpmyadmin上的laradock错误-无法连接

excel - Laravel 5.7 : SQLSTATE[22007]: Invalid datetime format: 1292 Incorrect date value: '35492' for column 'resident_dob' at row 1