繁体   English   中英

Nginx位置覆盖Laravel 5.2路线?

[英]Nginx location to override a Laravel 5.2 route?

我有一个nginx Web服务器,在端口80上提供Laravel 5.2应用程序。

通过访问mydomain.com,我的laravel应用程序启动,一切都按预期工作。

另一方面,我有一个在端口83上运行的nodejs应用程序,它提供其他类型的内容。 当我想通过主域上的反向代理服务我的nodejs内容时,问题出现了。

我想要做的是让nginx在domain.com/api/info/socket上运行我的nodejs app而不用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服务于特定网址而不让Laravel接管的任何想法?

尝试将location /api/info/socket块移到location /

一切顺利。 你只需要添加几行。

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
如果它解决了您的问题,请告诉我。 :)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM