简体   繁体   中英

Nginx location to override a Laravel 5.2 route?

I have an nginx web server serving a Laravel 5.2 app on port 80.

By going to mydomain.com my laravel app kicks in and everything is working as intended.

On the other hand, I have a nodejs application running on port 83 that serves other type of content. The problem comes when I want to serve my nodejs content throught a reverse proxy on the main domain.

What I'm trying to do is get nginx to serve at domain.com/api/info/socket my nodejs app without laravel trying to parse that url with it's routing system. Here is my nginx configuration to try and do so:

    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;
        }
    }

But everytime I visit that URL I get the laravel 404 error message since it's not a part of my routes configuration. Any idea on how to force nginx to serve that specific url without letting Laravel take over?

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

Everything is fine. You just need to add few lines.

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;
}

Also, you need to place your node application into public directory and don't forget to restart nginx sudo service nginx restart
Let me know if it solved your problem. :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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