Serving nodejs static files in the same domain as my PHP - php

I have website which running on PHP, but also I have NodeJS app which is running on the same domain.
NodeJs address is /nodejs, all other requests are under PHP.
PHP project located in folder /home/php-project
PHP static files folder /home/php-project/public
NodeJS is located in - /home/nodejs-project.
NodeJS static files folder /home/nodejs-project/public
Is it possible to serve different static folders?
Here is my NodeJs code
app.use(express.static(__dirname + '/public'));
Currently all static files coming from php static folder and I can't get access to nodejs static files.
Here is the nginx config
server {
listen 80;
server_name www.example.com example.com;
return 301 https://www.example.com$request_uri;
}
server {
listen 443 ssl;
server_name example.com;
#### Comodo SSL
ssl_certificate path/to;
ssl_certificate_key path/to;
return 301 https://www.example.com$request_uri;
}
server {
listen 443 ssl;
server_name www.example.com;
#### Comodo SSL
ssl_certificate path/to;
ssl_certificate_key path/to;
root /home/php-project/public;
index index.php;
location / {
autoindex off;
try_files $uri $uri/ /index.php?$query_string;
}
location /node-js {
proxy_pass http://localhost:5555;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_buffering off;
root /home/node-js;
}
} #### End server setup

Try this:
location / {
root /home/php-project/public;
try_files $uri $uri/ /index.php?$query_string #fallback;
}
location #fallback {
root /home/nodejs-project/public;
try_files $uri $uri/ =404;
}

Related

php is being downloaded instead of being loaded

I got a nginx docker container which actually works. Because when i do curl localhost:7070 on my host i get the content of the site as return. Then i made a config for nginx on my host, the problem is when i try to open the site in a browser it downloads the php file. I will sahre both configs of host nginx and docker nginx.
Nginx host conf:
server {
#listen 80 default_server;
#listen [::]:80 default_server;
# SSL configuration
listen 7171 ssl default_server;
listen [::]:7171 ssl default_server;
root /var/www/html;
# Add index.php to the list if you are using PHP
index index.php index.html index.htm index.nginx-debian.html;
server_name SERVERNAME;
location / {
proxy_pass http://127.0.0.1:7070;
#proxy_redirect off;
#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;
}
}
nginx docker conf:
server {
listen 80;
server_name default_server;
root /usr/share/nginx/selfoss;
index index.php index.html;
location ~* \ (gif|jpg|png) {
expires 30d;
}
location ~ ^/(favicons|thumbnails)/.*$ {
try_files $uri /data/$uri;
}
location ~* ^/(data\/logs|data\/sqlite|config\.ini|\.ht) {
deny all;
}
location / {
index index.php;
try_files $uri /public/$uri /index.php$is_args$args;
}
}
I know alittle about nginx normal config but im noob at php so what is the thing that im missing here?
I have seen posts about php-fpm and fastcgi but i have no clue ho or where to do that config changes.
You are missing a configured PHP-FPM in your Nginx-Config and of course a running PHP-FPM (inside the same Docker-Container or in a separate Docker-Container).
Example Nginx-Config inside your server{} section
location ~ \.php {
fastcgi_split_path_info ^(.+\.php)(/.*)$;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_buffers 16 16k;
fastcgi_buffer_size 32k;
include fastcgi_params;
proxy_read_timeout 300;
fastcgi_read_timeout 300;
fastcgi_pass unix:/var/run/sockets/php/www.sock;
}

Adding Flask app in existing nginx PHP configuration

The configuration below is the configuration for a web application in PHP, and it's working (I faked the site's name to https://sub.mysite.nl).
server {
listen [::]:443 ssl ipv6only=on; # managed by Certbot
listen 443 ssl; # managed by Certbot
## some certificate info ##
root /path/to/www;
index index.php index.htm index.html;
server_name sub.mysite.nl;
location / {
try_files $uri $uri/ =404;
}
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;
}
location ~ /\.ht {
deny all;
}
## some logging info ##
}
server {
if ($host = sub.mysite.nl) {
return 301 https://$host$request_uri;
} # managed by Certbot
listen 80 default_server;
listen [::]:80 ipv6only=on default_server;
server_name sub.mysite.nl;
return 404; # managed by Certbot
}
Now I want to add a Flask app in a subfolder e.g. https://sub.mysite.nl/flaskapp.
The block below is what I got from the Flask Mega Tutorial I followed, see specifically this chapter: https://blog.miguelgrinberg.com/post/the-flask-mega-tutorial-part-xvii-deployment-on-linux (under Setting Up Nginx). I think I need to put this under location /flaskapp/ but I'm not sure how to proceed, because when I do this and go to https://sub.mysite.com/flaskapp it gives me 404 Not Found.
location /flaskapp {
proxy_pass http://localhost:8000;
proxy_redirect off;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
Do I need to change the routing in my Flask app?
Ok, I've been fooling around a bit and it seems like editing the routes in my Flask app provides the easiest solution. For this, I use the Flask nginx block from my original post at the bottom.
So instead of #app.route('/'), I use #app.route('/flaskapp/').
And #app.route('/view_profile/<username>') becomes #app.route('/flaskapp/view_profile/<username>').

socket.io only works locally with nginx

(PHP runs with nginx, and I use socket.io from NODEJS)
If I try my website locally (using 2 web diffenrets web browzers), everything works.
But If I host my website (hosted in my house), I still can access to it with an other computer, but the functions of my app.js are not executed...
Here is my last error.log from nginx :
2016/05/03 14:11:00 [error] 25016#25016: *108 FastCGI sent in stderr: "PHP message: PHP Notice: Only variables should be passed by reference in /var/www/html/outer_treatment/game/add_message_discussion.php on line 55" while reading response header from upstream, client: 192.168.1.16, server: default, request: "POST /outer_treatment/game/add_message_discussion.php HTTP/1.1", upstream: "fastcgi://unix:/run/php/php7.0-fpm.sock:", host: "192.168.1.13", referrer: "http://192.168.1.13/game.php"
in my page where the NodeJS functions are handled: (located in : /view/game/index.php)
# into the <head>
<script src="/NODEJS/socket.io/socket.io.js"></script>
# into the <body>
var socket = io.connect('127.0.0.1:3000');
And my nodeJs file app.js : (located in : /NODEJS/app.js)
var app = require('express')(),
server = require('http').createServer(app),
io = require('socket.io').listen(server),
fs = require('fs');
io.sockets.on('connection', function(socket)
{
// here my functions
});
server.listen(3000, "127.0.0.1");
Here is my default file of Nginx (located in : /etc/nginx/sites-available)
# the IP(s) on which node server is running.
upstream app_default {
server 127.0.0.1:3000;
keepalive 8;
}
server {
# Default listen lines :
#listen 80 default_server;
#listen [::]:80 default_server ipv6only=on;
# NODE JS listen
#listen 0.0.0.0:80;
listen 80;
#root /usr/share/nginx/html;
root /var/www/html;
index index.php index.html index.htm;
#server_name localhost;
server_name default;
location / {
# First attempt to serve request as file, then
# as directory, then fall back to displaying a 404.
try_files $uri $uri/ /index.php;
# Uncomment to enable naxsi on this location
# include /etc/nginx/naxsi.rules;
#NODEJS configuration :
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://127.0.0.1:3000/;
proxy_redirect off;
# the websockets :
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
}
location ~ \.php$ {
try_files $uri /index.php =404;
fastcgi_pass unix:/run/php/php7.0-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
location ~ \.css {
add_header Content-Type text/css;
}
location ~ \.js {
add_header Content-Type application/x-javascript;
}
location ~ \.png$ {
try_files $uri $uri /$1;
}
}
Thanks you, I hope my problem is enough accurate
I finally resolved my problem, in the index.php file I ve modified
var socket = io.connect('127.0.0.1:3000');
by
var socket = io.connect('http://'+window.location.host+':3000');
everything works now ! :)

Wordpress running on nginx on a subdomain doesn't work on port 80

I have a server hosting two Django websites and a wordpress blog, all on nginx. I have setup one Django site to be served at the top level domain, and the other at a subdomain.
I have not been able to get the Wordpress site to work when it is on port 80. If I change the port to 8079, it works correctly.
Here is my nginx conf for the Wordpress site:
server {
listen 80;
root /var/www;
index index.php index.html index.htm;
server_name sub2.mydomain.com;
location / {
try_files $uri $uri/ /index.php?q=$uri&$args;
}
error_page 404 /404.html;
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/www;
}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
location ~ \.php$ {
try_files $uri =404;
#fastcgi_pass 127.0.0.1:9000;
# With php5-fpm:
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
include fastcgi_params;
}
}
One thing I have noticed is that even after I change the port back to 80 and restart nginx, when I go to the url in my web browser, it will redirect to port 8079. I'm not sure if that is caused by the browser cache, or DNS or something on my server.
Here are my other nginx configs:
upstream mydomain_app_server {
server unix:/webapps/mydomain/run/gunicorn.sock fail_timeout=0;
}
server {
listen 80;
server_name www.mydomain.com;
client_max_body_size 4G;
access_log /webapps/mydomain/logs/nginx-access.log;
error_log /webapps/mydomain/logs/nginx-error.log;
location /static/ {
alias /webapps/mydomain/static/;
}
location /media/ {
alias /webapps/mydomain/media/;
}
location / {
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_redirect off;
if (!-f $request_filename) {
proxy_pass http://mydomain_app_server;
break;
}
}
# Error pages
error_page 500 502 503 504 /500.html;
location = /500.html {
root /webapps/mydomain/static/;
}
}
and my last one is identical except with different paths and the following server_name:
server_name sub2.mydomain.com;

How to configure both php and nodejs app together using nginx on the same server

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.

Categories