簡體   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