Hello I'm using open source prerender middleware, as I installed it on CentOS 7 server as follows:
git clone https://github.com/prerender/prerender.git
cd prerender
npm install
The configuration I am using in server.js file is quite simple:
var prerender = require('./lib');
var server = prerender({
chromeLocation: '/usr/bin/google-chrome-stable',
chromeFlags: ['--no-sandbox','--headless', '--disable-gpu', '--remote-debugging-port=9222', '--hide-scrollbars','--disable-setuid-
sandbox' ]
});
server.use(prerender.sendPrerenderHeader());
// server.use(prerender.blockResources());
server.use(prerender.removeScriptTags());
server.use(prerender.httpHeaders());
server.use(require('prerender-memory-cache'))
server.start();
I'm also using the prerender-apache middware along with prerender and angular js app
RequestHeader set X-Prerender-Token "token"
<IfModule mod_proxy_http.c>
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{HTTP_USER_AGENT} Googlebot|Sitemaps|Google-Structured-Data-Testing-Tool|baiduspider|facebookexternalhit|twitterbot|rogerbot|linkedinbot|embedly|quora\ link\ preview|showyoubot|outbrain|pinterest|slackbot|vkShare|W3C_Validator [NC,OR]
RewriteCond %{QUERY_STRING} _escaped_fragment_
RewriteCond %{REQUEST_URI} /(.*)/(.*)/
# Only proxy the request to Prerender if it's a request for HTML
RewriteRule ^(?!.*?(\.js|\.css|\.xml|\.less|\.png|\.jpg|\.jpeg|\.gif|\.pdf|\.doc|\.txt|\.ico|\.rss|\.zip|\.mp3|\.rar|\.exe|\.wmv|\.doc|\.avi|\.ppt|\.mpg|\.mpeg|\.tif|\.wav|\.mov|\.psd|\.ai|\.xls|\.mp4|\.m4a|\.swf|\.dat|\.dmg|\.iso|\.flv|\.m4v|\.torrent|\.ttf|\.woff))(.*) http://some.ip:3000/https://%{HTTP_HOST}/$2 [P,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{HTTP_USER_AGENT} Googlebot|Sitemaps|Google-Structured-Data-Testing-Tool|baiduspider|facebookexternalhit|twitterbot|rogerbot|linkedinbot|embedly|quora\ link\ preview|showyoubot|outbrain|pinterest|slackbot|vkShare|W3C_Validator [NC,OR]
RewriteCond %{QUERY_STRING} _escaped_fragment_
RewriteCond %{REQUEST_URI} ^/rootDirectory/
# Only proxy the request to Prerender if it's a request for HTML
#RewriteRule ^(.*) http:/some.ip:3000/https://%{HTTP_HOST} [P,L]
</IfModule>
So first I was trying to run it with forever.js with following command in prerender folder:
forever start server.js
It works properly for a few hours and then a problem arises that I can't figure out why it occurred. Many chrome sessions start to be created and the server RAM starts to decrease until the server stops, I read that the flag: '--no-sandbox causes this problem and I have removed it, but then I got another problem when I start server.js forever I got :
Starting Prerender
Starting Chrome
Prerender server accepting requests on port 3000
Chrome connection closed... restarting Chrome
Chrome died immediately after restart... stopping Prerender
error: Forever detected script exited with code: 0
I'm using
node.js v12.14.0
Google Chrome 85.0.4183.102
I'll be grateful if someone could help thanks.
Related
I almost broke my brain while figuring it out and I need some help.
I have a shared hosting, Apache 2.4, example.com and a website running on main domain built in php.
I installed nodejs and it is running on example.com:5000 and it is working well, however I need nodejs to be able to listen to the subdomain app.example.com (and app.domain.com/*)
I have tried so many combinations of following things and none worked out.
Using vhost package for nodejs:
const vhost = require('vhost');
const express = require('express');
const app = express();
****
express()
.use(vhost('app.example.com', require('../widget/app.js').app))
.listen(port)
//it is only listening to the port, and doesn't work for the subdomain
Rewriting .htaccess file and saving it to the root folder or the public folder or the app folder:
variant 1:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^ app.example.com/$
RewriteRule ^(.*)$ http://example.com:50000/$1 [P,L]
Variant 2:
RewriteEngine On
RewriteCond %{SERVER_PORT} ^50000$
RewriteRule ^(.*)$ http://app.example.com/$1 [P,L]
Variant 3:
RewriteEngine On
RewriteRule ^$ http://app.example.com:50000/ [P,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ http://app.example.com:50000/$1 [P,L]
What is the right way to make it happened?
Have a great day!
I have set up Nodejs application(developed on ReactJs) in Cpanel. Everything works fine except I cannot directly browse to any route of the application. In Application Url I have the following setting Application URL : subdomain.domain.com . When I browse to subdomain.domain.com than everthing works fine. Even when I click some link to route to subdomain.domain.com\routeA, it works as expected.
But when I type subdomain.domain.com\routeA directly on the browser than I get status code 500 error. using Application URL : subdomain.domain.com/* doesn't work either. How can I achieve this.
You can add a .htaccess file similar to this:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.html$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule . /index.html [L]
</IfModule>
source: https://dev.to/crishanks/deploy-host-your-react-app-with-cpanel-in-under-5-minutes-4mf6
I'm just starting out with my first Next.JS app. I've used npx create-next-app and have made a few pages, when I realized that I'm not sure how to use a .htaccess file. I'm used to Apache taking care of this stuff for me, and simply putting my .htaccess file into my Next.JS app's root directory unsurprisingly didn't seem to cut it. How would I go about setting up a .htaccess file similar to the following?
RewriteEngine on
RewriteRule ^profile/([a-z0-9]+) profile.html
If You use next export to SSG on your project
This .htaccess file will fix redirection problem
RewriteEngine On
RewriteRule ^([^/]+)/$ $1.html
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)/$ $1.html
RewriteRule ^([^/]+)/([^/]+)/$ /$1/$2.html
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !(\.[a-zA-Z0-9]{1,5}|/)$
RewriteRule (.*)$ /$1/ [R=301,L]
.htaccess files are specific to Apache, so without Apache you can't use them. You can use Apache as a proxy to your node.js app, but you would still not use a .htaccess file; you could configure RewriteRules in your Apache config but there is no need to when you can handle all your routing directly in your application logic.
In node.js you don't need a separate web server like Apache. Your program can be long-running, bind to a port, and listen and respond to requests which is the main functionality that a web-server normally provides.
Next.JS has documentation for setting up custom routing here: https://nextjs.org/docs/#custom-server-and-routing
You have to learn to use pm2 :
You should be able to install nvm in your ubuntu, centos etc.. via ssl:
from https://github.com/nvm-sh/nvm
nvm will give you possibilities to install node
After you complete the installation of node, install pm2 globally,
https://pm2.keymetrics.io/
At root dir of project create file : ecosystem.config.js
ecosystem.config.js :
module.exports = {
apps : [
{
name: "your_server_name",
script: "./server.js",
watch: true,
env_development: {
"PORT": 3000,
"NODE_ENV": "development"
},
env_production: {
"PORT": 8001,
"NODE_ENV": "production",
}
}
]}
.htaccess look like this :
DirectoryIndex disabled
RewriteEngine On
RewriteRule ^$ http://127.0.0.1:8001/ [P,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ http://127.0.0.1:8001/$1 [P,L]
Login to your site over SSH:
ssh name#IP then password
How to run pm2 .
pm2 start ecosystem.config.js --env production
pm2 start ecosystem.config.js --env development
Which files u need in server :
If your server is already using Apache and it has mod_rewrite enabled, you can use this .htaccess:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.html$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule . /index.html [L]
</IfModule>
Source
The top answer didn't help me as I was using multiple levels of params....
Imagine the page pages/{...complex}.tsx, where complex is ["a","b","c"]. The following helped me:
DirectorySlash Off
RewriteEngine On
RewriteRule ^$ index.html [QSA,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.+?)(?<!\.html)\/?$ $1.html [QSA,L]
ErrorDocument 404 /404.html
As we know, when we run NodeJS on hosting server and want to hide port like :3000 from the browser address bar, we can set up ngnix or use in .htaccess the rules like
RewriteEngine On
RewriteRule ^$ http://example.com:3000/ [P,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ http://example.com:3000/$1 [P,L]
But what about running NodeJS locally on Windows? Is it possible to hide the port in the browser address bar in this case? I mean without additional server
ps. I run Windows binary 64-bit version of NodeJS on Windows 7
I'm using a shared hosting service that always has Apache web server running, so I can't run my Node.js application directly on port 80. Instead, as I've been told by my host, I need to use .htaccess to redirect incoming requests to my Node.js app, which is currently running on port 50000. Here's the .htaccess file they told me to use:
RewriteEngine On
RewriteRule ^$ http://127.0.0.1:50000 [P,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ http://127.0.0.1:50000/$1 [P,L]
This works well enough, except that when I try to go to mydomain.com, the Node app is seeing a request for /index.php. The tech support for my host seems to be as confused as I am. If I go to mydomain.com/test then Node.js app sees /test, so Apache seems to only be adding index.php on the root URL. Could this be an Apache caching issue from someone accessing the URL prior to the .htaccess file and Node.js app being set up?
UPDATE
At this point, no one seems to have a clue what is going on, so I'm just going to add an 'index.php' route to my Node app. Thanks to everyone who took a look and tried to help out.
You might have DirectoryIndex set up for index.php in apache conf file which may be the reason you are getting index.php automatically, what you can do is to set DirectoryIndex to some filename which may not exist or if it is apache 2.4 use DirectoryIndex disabled in your .htaccess.
This is actually what you are going to want to put in your /public_html directory
In the .htaccess file in the code below you will see
http://127.0.0.1:50000
(50000) is the port you are sending it too. There are 2 spots where you make that update.
Also update the example.com
RewriteEngine On
RewriteCond %{HTTP_HOST} ^example\.com [NC]
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://example.com/$1 [R,L]
DirectoryIndex disabled
RewriteEngine On
RewriteRule ^$ http://127.0.0.1:50000 / [P,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ http://127.0.0.1:50000 /$1 [P,L]
This is a specific configuration for an apache server with nodejs.