Single page applications Nginx Django Python and port forwarding - javascript

I want to run my single page app in production.
Since I'm using rest API and ajax calls to deliver the content from the backend to the front end for SEO reasons I need to have a no javascript version of my content available for the crawler bots.
I have a url for no javascript version of the content but since the webserver is running on port 8000 to access the no javascript files I need to navigate to mydomain.com:8000/nojs.
My apps url looks like the code below:
urlpatterns = [
url(r'^nojs/$', views.nojs),
url(r'^blog/all/$', views.allTitles),
url(r'^post/(?P<id>[\d+]+)/(?P<title>[\w+]+)/$', views.viewArticle)
]
and my nojs method is simply a hello message:
def nojs(request):
return HttpResponse("Hello")
I'm guessing I need to create some changes in my Nginx configuration to access the nojs method without using the 8000 port after my domain name.
How can I navigate to mydomain.com/nojs and get the hello message without using the 8000 port?

To access the files in the nojs folder I needed to add the following to the Nginx config file for the domain and that would forward the traffic from port 8000 to port 80.
location /nojs {
proxy_pass http://127.0.0.1:8000/nojs/; #or whatever port you are using
proxy_set_header Host $host;
}

Related

GCP deployment port issue for a node js project

I have deployed one node js project in which a http server is created at port 8080 and listening at 8080 and a service url is generated.But inside http.createServer i am creating another server for a nlp engine which is listening at port 8081.So using the service url generated after deployment i am able to hit server running at 8080,but how to access the nlp engine server running at 8081 using the same service url with out using router?ordo gcp doesnt allow creation of two server listening at two ports with the same service url?
Can you try http://hostname:8081? what does it say?
If the port 8081 is open, then ideally you should be able to access that NPL engine, else you may need to get the port open in GCP.

Nginx not loading the second angular app

Being new to Angular 4, Nodejs and Nginx i'm having trouble configuring Nginx to serve 2 different Angular apps. My problem is the following:
I generated two Angular 4 apps with the angular CLI and then modified them. Once done, I did a ng build on both of them to get the built files in the dist folder. The first app is the login page that does JWT Authentification with a backend in Nodejs. Once the auth is done it should redirect the user to the second app which is the actual website.
I use window.location.href='http://localhost:8080/app' in the login app to redirect to the second one.
Now since the second app is supposed to check the Token, it is stored in the browser's localStorage.
In order to keep this token, both the apps must be on the same address and port. Therefore I want to use NginX as a reverse proxy.
I installed Nginx and put the files of both apps in /var/www/html/angular/login/ and /var/www/html/angular/app/ respectively.
My Nginx sites-available/default file is the following:
#Default server configuration
server {
listen 8080 default_server;
#listen [::]:8080 default_server;
index index.html;
server_name localhost:8080/;
location / {
alias /var/www/html/angular/login/;
try_files $uri $uri/ /index.html;
}
location /app {
alias /var/www/html/angular/app;
try_files $uri $uri/ /index.html;
}
}
Now what's happening is that when I go on localhost:8080 I get my login app just as I should but when I authenticate, the url changes to localhost:8080/app as it should and the name of the app on the tab changes but the content of the app isn't loaded. It stays on the login page eventhough it seems to fetch the second app files...
It looks as if I had put the files of the login app in the /var/www/html/angular/app/ folder eventhough I didn't.
Any idea where it comes from?
The apps were tested before using ng serve on 2 different ports but I couldn't keep the token since localStorage is attached to a domain and port.

Point to node middleware without using port

I have a website, let's say mywebsite.com
I am using node and express for the middleware and point to mysite.com:3011/api
To make my middleware calls.
Hosting the site statically in ubuntu 16 (Linux) and running the middleware separately using pm2 (node server)
I would like to simply be able to do something like mysite.com/API without specifying a port in the API call.
Today I tried to demo the site at a corporate office and the apis failed due to not allowing a port to be specified in the URL.
You can use a reverse proxy (like nginx) to hide this port and forward the request to your Node.js api.
Something like this:
server {
listen 80;
...
location / {
root /path/to/static/files;
}
location /api {
rewrite ^/api(.*) /$1 break;
proxy_pass http://127.0.0.1:3011;
}
...
}

deploying node add on server without using localhost

I have a node app that I am trying to deploy on my server. I have an index.html file in a public folder and an app.js file. If I navigate to the project in the command line and run node app.js it runs the app on localhost:8888 and shows the index.html file.
Now that I have uploaded this to my server I am wondering what I need to do, and change (if anything) in my app.js file so that i can visit the site without visiting localhost:8888, but instead the actual url.
I have tried http://162.xx.xxx.xxx/folderName/app/public:8888, but this doesn't work.
var express = require('express')
var app = express();
app.use(express.static(__dirname + '/public'))
app.get('/', function (req, res) {
res.send('Hello World!')
})
app.listen(8888, function () {
console.log('Example app listening on port 8888!')
});
"Server" is a word with two primary meanings in software development.
It can mean either "A piece of software that listens on a network" or "A computer running that kind of software".
So having uploaded the JavaScript program to the remote computer that is your server you need to do exactly the same as you did on your own computer.
i.e. you need to get a terminal on the server and run node app.js
It will then be available at http://your.example.com:8888/
(More advanced uses would involve using software like forever or system.d to run it automatically as a background process).
If you were using the term server with the other meaning (i.e. you mean "Apache HTTP" or "IIS" or similar), then you are out of luck.
Using Node for server side code means running a server written in JavaScript.
To use this in combination with something like Apache, you would either:
Run the Node server instead of Apache
Run the Node server on a different port and point some services at that port explicitly
Run the Node server on a different port and use something like ProxyPass to have Apache relay requests to it
Change the port number from 8888 to 80 and then use the address of your server in the browser. For example, "mysite.com" for a domain name or "123.45.678" for an IP address.
If there are other sites on that server, you can't run it on port 80. (Port 80 is the default port websites use.) You'd need to use a different port. So, say you kept 8888 -- the address would be yoursite.com:8888

How to configure my node.js application on a dedicated server to put it online?

I've installed node.js on my server(it is a virtual windows server). I am also having the domain. I want to run my node.js application on a port 8001 so that when I open, say http://example.com:8001/ it will open my application.
Actually, I am also having a PHP site running on Apache server on port 80(XAMPP). It is perfectly working fine when I open say, http://example.com.
Thanks
In apache, create a new vhost. You have to proxy all requests through apache to your node app as apache is listening to port 80.
<VirtualHost *:80>
ServerName example.com
ProxyRequests off
<Proxy *>
Order deny,allow
Allow from all
</Proxy>
<Location />
ProxyPass http://localhost:8001/
ProxyPassReverse http://localhost:8001/
</Location>
</VirtualHost>
If you are not on a name-based virtual host environment you can just start your Node.js server and it will be available to the world from the defined port. But if the server IP address runs many services using different domain names and you want your Node.js server to reply only from http://example.com:8001 you can use e.g. the vhost module on your node server to listen to specific domain only:
var express = require('express'),
host = require('vhost');
var app = express();
app.get('/', function(req, res) {
res.send("Hello from vhost");
});
var main = express();
main.use(vhost('example.com', app));
if (!module.parent) {
main.listen(8001);
}
You can also proxy the requests through a web server like Apache or nginx, but to have the service responding from port 8001 you would need to bind the web server to port 8001 in addition to 80 you are already using, and run your Node.js server on some other port.
Generally people prefer using the standard HTTP port and use a web server to reverse proxy traffic to the Node.js server running on a non-privileged port. As you already have the PHP application on your domain name you would then follow advice by #SamT and run your Node.js application from e.g. http://mynodeapp.example.com

Categories