0

I have the following nginx config

server {
        listen 80;
        client_max_body_size 2M;
        server_name some.app;
        root /var/virtual/a-cakephp-app/webroot;
        access_log /var/log/nginx/a-cakephp-app-access.log;
        include common.conf;
        include cakephp.conf;
        location /billing/ {
                proxy_set_header X-Real-IP $remote_addr;
                proxy_set_header HOST $http_host;
                proxy_set_header X-NginX-Proxy true;

                proxy_pass http://127.0.0.1:89;
                proxy_redirect off;
                rewrite ^/billing/(.*)$ /$1 break;
        }

And my webapps are:

/var/virtual/a-cakephp-app ==> virtual path that leads to the cakephp folder (definitely working)

/var/virtual/a-laravel-app ==> virtual path that leads to the laravel folder (not too sure how to test it)

What I want to achieve

I have a cakephp 2 app that is running at http://some.app. What I want is to start another app running Laravel at http://some.app/billing

My Laravel .env

APP_ENV=local
APP_DEBUG=true
APP_KEY=base64:somekey
APP_URL=http://some.app/billing

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306

What did I get

I got a bad gateway error

What did I expect

I was hoping that the laravel app can work without compromising the cakephp app

Kim Stacks
  • 10,202
  • 35
  • 151
  • 282
  • where did you put your laravel folders, I mean the path – msonowal Jan 09 '17 at 05:58
  • answered above. basically /var/virtual/a-cakephp-app ==> virtual path that leads to the cakephp folder (definitely working) /var/virtual/a-laravel-app ==> virtual path that leads to the laravel folder (not too sure how to test it) – Kim Stacks Jan 09 '17 at 08:46

4 Answers4

0

You need to tell nginx that the root is different for /billing/ paths:

...
location /billing/ {
    ...
    root /var/virtual/a-laravel-app/public;

    # this replaces the rewrite
    # rewrite will alter the url in nginx and a new lookup will be made
    # the entry point for Laravel is the public/index.php file
    try_files /index.php =404;
    ....
}
...
dkt
  • 146
  • 3
0
  1. It is generally a bad idea to mix and match independent apps on a single domain, which may lead to security issues w.r.t. cookies.

  2. You're trying to use a proxy_pass for Laravel, a PHP framework. Note that this directive is meant to be used when the request is meant to be passed, in HTTP over TCP form, to a subsequent HTTP server, running on the specified port (e.g., Apache Tomcat, Jetty etc).

    PHP (and hence Laravel) can be run by nginx itself, and doesn't require a separate server, so, your proxy_pass setup is likely a mistake, and should have been an appropriate fastcgi_pass set of directives instead (the normal way php is executed from within nginx).

  3. We have no idea what is in the files that you include, however, it would be a good guesstimate that they do contain a location ~ \.php$ directive (for handling the php files of your existing php app).

    Note that as per http://nginx.org/r/location, a location with a regular expression like location ~ \.php$ will take precedence over a prefix-string location like location /billing/, when a file like /billing/index.php is accessed.

    To modify such behaviour, use the ^~ specifier for the prefix string location, e.g., location ^~ /billing/.


In summary, I'd use a separate domain. Else, use location ^~ /billing/, and fit all the proper fastcgi within.

cnst
  • 25,870
  • 6
  • 90
  • 122
  • Thanks for your answer. But I have no idea what the fastcgi are. But I appreciate your well documented answer. I solved it in a different way and will share it in this question at http://stackoverflow.com/a/41982660/80353. Also gave you the bounty to thank you. I also note your comment about the security issue. i wasnt aware of that at first. Thanks for educating me. This is what I love about learning on SO. – Kim Stacks Feb 01 '17 at 14:46
0

I kept the original config as such:

server {
        listen 80;
        client_max_body_size 2M;
        server_name some.app;
        root /var/virtual/a-cakephp-app/webroot;
        access_log /var/log/nginx/a-cakephp-app-access.log;
        include common.conf;
        include cakephp.conf;
        location /billing/ {
                proxy_set_header X-Real-IP $remote_addr;
                proxy_set_header HOST $http_host;
                proxy_set_header X-NginX-Proxy true;

                proxy_pass http://127.0.0.1:89;
                proxy_redirect off;
                rewrite ^/billing/(.*)$ /$1 break;
        }

Then I created this config after that:

server {
        listen 89;
        client_max_body_size 2M;
        server_name 127.0.0.1;
        root /var/virtual/a-cakephp-app/another-laravel-app/public;
        include common.conf;
        include cakephp.conf;
}

Note the port 89 does not matter so long as the server is not using it for some other app

Kim Stacks
  • 10,202
  • 35
  • 151
  • 282
  • Heh, that's a really hacky/inefficient way to go about it! A better option might be to use variables in `root`, and set those within the individual locations. – cnst Feb 01 '17 at 20:04
  • @cnst variables? i am not that good with nginx config. can give an example? i will experiment with it. – Kim Stacks Feb 02 '17 at 00:38
  • You cannot use `root` within an `if` statement, but you could change variables to the exact same effect; see http://stackoverflow.com/questions/38038689/how-to-conditionally-override-a-header-in-nginx-only-if-a-cookie-exist/38154810#38154810 – cnst Feb 02 '17 at 04:06
  • i didn't even know it was possible to write if statements in nginx config! so roughly what should i write? pseudo code is enough, so I can try to explore – Kim Stacks Feb 02 '17 at 14:47
-1

Ideally you should not mix all the configurations under one file. nginx.conf should only only contain universal configurations like gzip being on, not to give out server tokens, etc. The individual file should be under sites-enabled folder

On why the bad gateway error is coming is maybe because you should have one root and then multiple location blocks to handle the same.

Also, once inside the billing block, are you trying to rewrite it to remove the billing folder? Why?

Karan Shah
  • 1,304
  • 11
  • 15
  • I wrote this under its own individual conf – Kim Stacks Jan 09 '17 at 14:18
  • I am not rewriting to remove. I just dun want to use another domain for the laravel app. I want the laravel app to be under a some.app/billing while the rest of some.app is handled by the older cake2 – Kim Stacks Jan 09 '17 at 14:19