Welcome! Log In Create A New Profile

Advanced

Nginx Handle Java and PHP at same context path

Posted by rishabh_6 
Nginx Handle Java and PHP at same context path
August 19, 2019 02:19AM
Hi there,

We have a server where we have two applications running simultaneously - Jetty/Java and PHP. Both are on same port 443.

Nginx listens Request and forward all requests to Java starting / .
Nginx forwards all request to PHP starting from /home using Location.

Is it possible for nginx to handle both Java and PHP on / . SO that if either of java or PHP returned 404, Nginx forwards it to other.

Let say if Java returned 404 Nginx may forward it to PHP app i.e /home
Or If PHP returned 404 and Nginx forwards request to Java.

Is it possible to do in Nginx by use of if -else or some kind of error handling 404.

We want to serve all PHP pages on / too rather than /home, problem is Nginx is configured for java/jetty to listen on / using location.

PHP
====
location /home {
alias /opt/mySite.com/home;
try_files $uri $uri/ /home/index.php;
add_header 'Access-Control-Allow-Origin' '*';
add_header 'Access-Control-Allow-Headers' 'Access-Control-Allow-Headers, Origin,Accept, X-Requested-With, Content-Type, Access-Control-Request-Method, Access-Control-Request-Headers';
allow all;
location ~ \.php$|\.html$|\.js$ {
include snippets/fastcgi-php.conf;
fastcgi_param SCRIPT_FILENAME $request_filename;
fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;
}
}


location @home {
rewrite /home/(.*)? /home/index.php?/home$1 last;
}

location ~ /\.ht {
deny all;
}



JAVA
=====
location / {
add_header 'Access-Control-Allow-Origin' '*';
add_header 'Access-Control-Allow-Headers' 'Access-Control-Allow-Headers, Origin,Accept, X-Requested-With, Content-Type, Access-Control-Request-Method, Access-Control-Request-Headers';
proxy_pass http://localhost:8080/;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forward-Proto http;
proxy_set_header X-Nginx-Proxy true;
proxy_redirect off;
proxy_read_timeout 31104000;
proxy_connect_timeout 31104000;
proxy_send_timeout 31104000;
}
}


Regards,
Rishabh



Edited 1 time(s). Last edit at 08/19/2019 02:21AM by rishabh_6.
Sorry, only registered users may post in this forum.

Click here to login

Online Users

Guests: 176
Record Number of Users: 8 on April 13, 2023
Record Number of Guests: 421 on December 02, 2018
Powered by nginx      Powered by FreeBSD      PHP Powered      Powered by MariaDB      ipv6 ready