I am really confused whether it is possible ? Please help me out, I have Node.js application, say node_app, running on X port, and PHP application, say my_app, running in Apache's default 80 port. I have one domain name only. What my problem is, if user hit domain.com/my_app, it should run the PHP application in 80's port. If the user hits domain.com/node_app, it should run the node application in X port. And one more important constraint is the end-user should not see any port number in URL bar.
You can install Node.JS and PHP in the same host, using Nginx as proxy per exemple.
Per exemple, with Nginx, you could create two virtualhosts :
Default virtual host using PHP (FPM or not) who points to exemple.tld
Second virtual host to another node.exemple.tld
First VH is gonna be like this (with PHP-FPM) :
server {
listen 80; ## listen ipv4 port 80
root /www;
index index.php index.html index.htm;
# Make site accessible from exemple.tld
server_name exemple.tld;
location / {
try_files $uri $uri/ /index.php;
}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000 and using HHVM or PHP
#
location ~ \.(hh|php)$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_keep_conn on;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
location ~ /\.ht {
deny all;
}
}
Second VH with NodeJS :
server {
listen 80;
server_name node.exemple.tld;
location / {
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_set_header X-NginX-Proxy true;
access_log off;
# Assuming that NodeJS listen to port 8888, change if it is listening to another port!
proxy_pass http://127.0.0.1:8888/;
proxy_redirect off;
# Socket.IO Support if needed uncomment
#proxy_http_version 1.1;
#proxy_set_header Upgrade $http_upgrade;
#proxy_set_header Connection "upgrade";
}
# IF YOU NEED TO PROXY A SOCKET ON A SPECIFIC DIRECTORY
location /socket/ {
# Assuming that the socket is listening the port 9090
proxy_pass http://127.0.0.1:9090;
}
}
As you can see, it's possible, and pretty easy to do!
Related
I'm doing a little project to learn Docker - NextJS + PHP backend with Nginx.
Now I got to a stage when I can say it works somehow. When reaching the API via browser adress bar, it gives me the error page - That's right. Nginx not complaining.
BUT when I try to FETCH it, it gives me file not found from the PHP-FPM container and the Nginx complains FastCGI sent in stderr: "Primary script unknown" while reading response header from upstream
The site config is this
upstream next_app {
# NextJS running app port
server nextapp:3000;
}
upstream php_fpm {
# PHP FPM server URI and port
server phpapp:9000;
}
server {
listen 80 default_server;
server_name _;
server_tokens off;
error_log /var/log/nginx/error.log;
access_log /var/log/nginx/access.log;
location ~ ^/api {
root /var/www;
try_files /www/index.php =404;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_pass php_fpm;
}
# proxy pass for NodeJS app
proxy_http_version 1.1;
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-Forwarded-Proto $scheme;
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
location / {
proxy_pass http://next_app;
}
}
The basic idea was to use PHP api on any request with query starting with /api.
I have a little suspicion on the Next app serving me different Fetch, but I didn't found anything to help it
Any idea how to make it work? Thanks
So, solved now, thanks to #ad7six comments I realized the problem is more accurately described by
"How the hell can I print out a Nginx variable when everything around fails?"
There's two ways - use return and it comes with the response body or
add_header with the always attribute.
add_header X-cgiscript 42 always;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME /var/www/www/index.php;
fastcgi_pass php_fpm;
So now the FPM only executes the index.php of the whole app.
Thanks
Good day,
My problem is that I am ONLY getting my Docker network IP address when using Laravel's $request->ip() or $request->getClientIp () or $request->server->get('REMOTE_ADDR') or $_SERVER['REMOTE_ADDR'] which essentially all do the same I assume.
So far the problem occurs on my local test pc (without Nginx Reverse Proxy) as well as on my live servers
I hope I've supplied enough relevant info below, if not please let me know :) Thanks in advance!
So my setup is the following (simplified), both Nginx configs are pasted below as well:
Server -> Nginx Reverse Proxy -> Docker Containers & Network (db, php, nginx, redis) -> Nginx Webserver <-> PHP-FPM
Some results related to _SERVER:
[SERVER_ADDR] => 172.20.0.3 (nginx webserver)
[REMOTE_PORT] => 55378
[REMOTE_ADDR] => 172.20.0.1 (docker network / gateway)
[SERVER_SOFTWARE] => nginx/1.19.6
I have read multiple forums and solutions for hours and hours while trying and changing configs etc. but none of it seems to be working.
What I have tried:
Get User IP address in laravel with similar method to HTTP_X_FORWARDED_FOR
https://serverfault.com/questions/618456/getting-the-client-ip-when-passing-through-a-reverse-proxy
So I have also tried to set my "Trusted Proxy" in Laravel's /Http/Middleware/TrustProxies.php by allowing all: protected $proxies = '*';
I also don't need these additional Laravel packages as of >5.5 I believe this is a built in feature of Laravel (TrustProxies).
Please find below my Nginx webserver config:
server {
listen 80;
server_name domain.com;
return 301 https://www.example.com;
}
server {
listen 80 default_server;
index index.html index.htm index.php;
server_name www.example.com;
root /var/www/public;
location ~ \.php$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass php-fpm:9013;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME /var/www/example.com/public/$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info;
}
# Support Clean (aka Search Engine Friendly) URLs & enable Gzip compression:
location / {
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
try_files $uri $uri/ /index.php?$query_string;
gzip_static on;
}
# Override the load balancer IP with real IP.
fastcgi_param REMOTE_ADDR $http_x_real_ip;
error_log /var/log/nginx/error.log;
access_log /var/log/nginx/access.log;
}
And my server's Nginx Reverse Proxy config (IF relevant):
server {
listen 80;
listen [::]:80;
server_name example.com.au www.example.com.au;
location / {
proxy_pass http://localhost:8013;
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-Host $server_name;
}
}
Ok figured it out! :D
I was using Cloudflare as a proxy, my DNS is with CloudFlare and I have enabled the "Proxy through CloudFlare" option due to speed / caching improvements.
This resulted in CloudFlare "injecting" a HTTP_CF_CONNECTING_IP into my header.
To retrieve this header value I have used $_SERVER['HTTP_CF_CONNECTING_IP'] in my code.
Another solution was to use the HTTP_X_FORWARDED_FOR value from the header by using _SERVER['HTTP_X_FORWARDED_FOR'], this I believe would, in my situation only work after doing the following (posted in my question):
TrustProxies.php
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
I hope this helps others as well :D
I am an amateur front end web developer, and I recently bought a Ubuntu server to try to my hand at some backend development. I am trying to figure out how to serve a php file from an aliased location block using php5-fpm. I am getting a 404 - Page not found error. I have tried all of the proposed solutions I could find here with no luck. As I am still a beginner I would love a quick ELI5 as well and any pointers on the rest of my conf file, so I can learn something too. I should mention that the main root folder is running a flask app, and is the reason I am using an aliased location.
My virtual host:
Nginx conf file
server {
listen 80;
listen [::]:80;
server_name www.example.com example.com;
root /var/www/example;
large_client_header_buffers 8 32k;
access_log /var/www/example/logs/access.log;
error_log /var/www/example/logs/error.log;
location / {
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $remote_addr; #$proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_set_header X-NginX-Proxy true;
proxy_pass http://app_test;
proxy_redirect off;
}
location /test_site {
alias /var/www/test_site;
index index.php index.html index.htm;
location ~ .php$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+?\.php)(/.*)?$;
fastcgi_pass unix:127.0.0.1:9000;
fastcgi_index index.php;
include fastcgi_params;
}
}
php5 www.conf file
[www]
...
user = www-data
group = www-data
listen = 127.0.0.1:9000
#listen = /tmp/php5-fpm.sock
listen.owner = www-data
listen.group = www-data
listen.mode = 0660
...
My fastcgi_params file is default. I have checked both the php and nginx logs and there are no errors. Any help would be much appreciated!
Getting alias to work with nested locations using fastcgi is complicated.
Assuming that you have not over simplified your configuration, the test_site location does not need to use alias:
location /test_site {
root /var/www;
index index.php index.html index.htm;
location ~ \.php$ {
try_files $uri =404;
fastcgi_pass unix:127.0.0.1:9000;
include fastcgi_params;
}
}
This removes the alias directive, and solves the aliasing problem in the PHP block.
Note also: The regex on the location ~ \.php$ block was wrong. The fastcgi_split_path_info and fastcgi_index directives are unnecessary.
The nginx directives are documented here.
I want Nginx to serve both my jenkins server and my Laravel (php) application. Here is my Nginx configuration:
server {
listen 80 default_server;
server_name localhost;
location / {
proxy_set_header Host $host:$server_port;
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;
# Fix the "It appears that your reverse proxy set up is broken" error.
proxy_pass http://127.0.0.1:8081;
proxy_read_timeout 90;
proxy_redirect http://127.0.0.1:8081 https://jenkins.domain.tld;
}
}
server {
listen 9000;
listen [::]:9000;
root /var/www/my-app/public;
index index.php index.html index.htm;
server_name my-app.io;
location / {
try_files $uri $uri/ =404;
}
error_page 404 /404.html;
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
location ~ \.php$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
I am using virtual-box as my virtual machine emulator for ubuntu 14. I am able to access jenkins application by going to http://127.0.0.1:8081 (I have changed the jenkins default port).
But when I am trying to access http://127.0.0.1:9000 I am getting an error, I have checked Ngnix logs but couldn't find any information regarding a request to port 9000.
Also I have made port forwarding in virtual-box, here is a screenshot:
Any help would be great.
Use server_name localhost too for php.
I have an Ubuntu VPC. I have a nodejs app running on it in some folder. I have a php app running on apache in var/www. What I want is to configure nginx so that when a user comes on mydomain.com , he/she is redirected to my node app running on port 3000 and when the user visits mydomain.com/docs , he/she is redirected to my php app running on apache on port 80.
server {
listen 80;
root /var/www;
location / {
try_files $uri $uri/ /index.html;
}
location ~\.php$ {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
include fastcgi_params;
}
location /doc/ {
alias /usr/share/doc/;
autoindex on;
allow 127.0.0.1;
deny all;
}
location ~/\.ht {
deny all;
}
}
My conf file for nodejs app is:
upstream app_somedomain {
server 127.0.0.1:3000;
}
server {
listen 0.0.0.0:80;
server_name mydomain.com mydomain;
root /some/path;
index index.html index.htm index.php index.js;
location / {
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_set_header X-NginX-Proxy true;
proxy_pass http://app_somedomain/;
proxy_redirect off;
}
}
I am using nginx for the first time. I am still unable to understand how to achieve this. As in how to serve my nodejs app running on port 3000 to my domain name and mydomain/docs to my php app.
Thanks.