2016-10-07 117 views
1

我有一个nginx web服务器,服务端口80上的Laravel 5.2应用程序。Nginx位置重写Laravel 5.2路由?

通过去mydomain.com我的laravel应用程序踢,一切都按预期工作。

另一方面,我有一个nodejs应用程序运行在端口83上,它服务于其他类型的内容。 当我想通过主域上的反向代理服务我的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; 
     } 
    } 

但每次我访问该网址我得到的laravel 404错误消息,因为这不是我的路线配置的一部分。有关如何强制nginx在不让Laravel接管的情况下提供特定网址的想法?

+0

您有权限为子域创建DNS条目吗?可以为你简化它。 – Blake

+0

这当然可以帮助,但如果可能的话,我想将所有公共端点保留在domain.com/api下 – Kanadeko

+0

可以使用'api.domain.com'并转发主域上的请求。是不是'proxy_set_header Host $ http_host;'?你在'localhost'或'127.0.0.1'上听吗? – Blake

回答

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
让我知道,如果它解决您的问题。 :)

+0

我做了这个改变,但我仍然有同样的问题。如果我发布我的整个默认配置而不仅仅是相关的服务器块,会更好吗? – Kanadeko

+0

我刚更新了其他信息的主要问题。我可以指出,/ stream不会被laravel解析并按预期工作,这很奇怪。我也用** sudo systemctl restart重启nginx **,不知道它是否改变了任何东西。 Ubuntu 16.04 – Kanadeko

+0

好的,告诉我你想成为什么样的网址? 'mydomain.com/api'或'mydomain.com/api/info/socket'?以及您尝试访问的网址是什么? – imrealashu

0

尝试移动location /api/info/socket块上方location /

+0

刚刚尝试过,我得到了完全相同的问题,Laravel的404页面。我还通过添加=来修改位置块,正如文档所建议的完全匹配一样,但它也不起作用。 – Kanadeko

+0

替换'try_files $ uri $ uri//index.php?$ query_string;' 与 'try_files $ uri $ uri /index.php$is_args$args;' – astratyandmitry

+0

不幸的是,这并没有解决问题:( – Kanadeko