How can I serve static files with 404 redirection by PM2? - pm2

I already know that there is a simple command to serve static folder by PM2:
pm2 serve <path> <port>
But how can I add 404 redirection to it? e.g. redirect to 404.html while that happends. Can't find that on Google & PM2 doc.

By default, pm2 displays 404.html from the serving directory if it can't map a request to any static file of that directory. For example, assume that your static file directory name is foo, which contains three images - 1.jpg, 2.jpg, 3.jpg. You are serving those files using:
pm2 serve <path_of_foo> 8080
Now, if the server receives any request like http://localhost:8080/bar.jpg, pm2 will look for a file named 404.html on foo directory and display its contents, as bar.jpg does not exist. If it doesn't find any, it show a simple text message 404 Not Found. There is no options to make it configurable (CLI or JSON), as per my knowledge.
However, if you make this file path configurable, you can take a look at here and customize the source according to your need.

Related

Index.html without XAMPP

Is it possible to automatically load index.html on a system folder without using XAMPP, IIS or similar?
It is for a school project and I can't use them, so I have to open the file putting the path (C:/...) into the address bar.
I know I could use .htaccess, but I don't know what to write and if it gets read without any web server solutions!
This can get a little tricky... but is possible without any "administrator" privileges, nor without installing anything.
Download Python 3.8.2 - Windows x86-64 embeddable zip file
Create a folder on "python" on the c:\
Extract the "Zip" file into this folder
Change the folder name from "python-3.8.2-embed-amd64" to "python_src"
Create a folder named "python_html"
The folder structure should look like:
c:\python\
c:\python\python_src\
c:\python\python_html\
Create a file named "webserver.py" in the "c:\python\python_html" folder
Place the following code into that file:
#webserver.py
import http.server
import socketserver
PORT = 80
Handler = http.server.SimpleHTTPRequestHandler
with socketserver.TCPServer(("", PORT), Handler) as httpd:
print("serving at port", PORT)
httpd.serve_forever()
Save and close the file
Create index.html file in the "python_html" folder and place the following code in that file:
<html>
<head>
<title>Web Title</title>
</head>
<body>
<h1>Python Web Server File</h1>
<p>Congratulations! The HTTP Server is working!</p>
</body>
</html>
Open the "Command Prompt" and type the following commands
cd\
cd python\python_html\
c:\python\python_src\python ./webserver.py
Open a web browser and navigate to "http://localhost/"
Once you have confirmed this works, you can build an entire website within that "python_html" folder. As long as you don't close the command prompt it will continue acting as a "Web Server".
I know I could use .htaccess
.htaccess is an Apache (Web Server) config file, so unless you have Apache installed (ie. the "A" in XAMPP) then you can't use that. (If .htaccess was available then index.html would likely load automatically anyway.)
On Apache, being able to load index.html by default when requesting a directory requires mod_dir (an Apache module). In this case, mod_dir issues an internal subrequest for the DirectoryIndex - this all requires additional processes.
I can't install extensions... I have to open the file on my school computer
If you can't install anything then you can't do this I'm afraid. You appear to be limited to direct file requests.
When using a webserver (such as Apache or IIS) then you have a differentiation between a URL and a filesystem path. The webserver maps the URL to a filesystem path. Without a webserver you don't have that abstraction.
There are lighter webservers, other than Apache and IIS, but you need to install something extra.
Just give your file(s) meaningful names (ie. not index.html) and use those instead? eg. fox-project.html

Have index.html file but still getting a directory listing

I have an index.html file in my Apache DocumentRoot directory but when I go to my URL, I am still getting a directory listing of my DocumentRoot directory instead of the index.html file being displayed. The apache access_log shows 200's when I reload the page. Any suggestions?
Use
DirectoryIndex index.html
It tells apache what document to show for a directory request.
update
You should specify just the filename that apache will look for in the folder requested.
Not saying this will fix it for you, but for me when first getting started with Apache2 it was file permissions that would get forgotten when moving or writing new file under the web root directory
ls -hal /var/www/host_one/index.html
If above doesn't have read (r) permissions for the same user:group or if the ownership doesn't include the user/group of the web server, then try the following for allowing group reads
# Modify ownership, change 'www_host' to Apache2 group
chown ${USER}:www_host /var/www/host_one/index.html
# give read+write (6) to user and read (4) to group owners
chmod 640 /var/www/host_one/index.html
Try refreshing the website and see if permissions where the issue. Note most web documents only require read permissions and ownership to be correct for browsers to be allowed to pick them up for rendering, on rare occasions you may need executable (1 or x) permissions for server scripts (be cautious of ownership in such cases) and last write permissions (2 or w) should likely never be seen without good reasons on files within your web root.
Second thing to try, use the index.html within your browsers URL bar
# by IP
http://192.168.0.100/index.html
# by domain
http://site-name.local/index.html
If the above loaded your document then, like #Pekka 웃 stated already, you've likely got a server option that's missing or enabling directory listings instead of looking for a index page within that directory. If this is the problem then there's two ways of fixing it that I've tried in the past. One, htaccess configuration to disable directory listing within that sub-directory, two, server vhost configuration to prevent whole site from directory listings. Personally I prefer to use option two and then on directories that should be allowed to be listed place an htaccess config for permissions instead of denials.

URL is not showing 404 page

I have a domain domain.com
And when I type domain.com/something.html/new/one/square/new.html I am not getting a 404 error.
Where something.html is present in the account correctly.
Directory new is present in my document root.
Square is not present in my account.
By default, if part of the url points to a file, the rest of the url is treated as so-called "path info". In php you should be able to get this path info by checking $_SERVER['PATH_INFO']. If you don't want Apache to work like this, turn path info off in your main config file (httpd.conf) or in .htaccess in your root directory with the following directive.
AcceptPathInfo Off
For more information, please check the documentation for AcceptPathInfo.

Cannot find URL error while it is definitely there

I have small CGI script running on a server[Linux OS]. following is a part of script output..
<tr><td valign="center">Lol</td><td valign="center">10112</td><td>abc.pdf</td></tr>
But when I click on this abc.pdf hyperlink, browser displays error message:URL /home/pathtopdf/abc.pdf was not found on the server. while the pdf and path is definitely there and all files and folders in the path[including pdf] has full permission.
My server location is # /srv/www and script in /srv/www/cgi-bin, but when I put the link to pdf as follows
<tr><td valign="center">Lol</td><td valign="center">10112</td><td>abc.pdf</td></tr>
The error message was The requested URL '/srv/www/for_html/abc.pdf' resolves to a file which is marked executable but is not a CGI file; retrieving it is forbidden. Again permission is there for files.
What could be the problemo?
Your problem is that you try to request a file outside of the webroot. So by clicking that, the browser is really requesting
http://example.com/home/pathtopdf/abc.pdf
not
/home/pathtopdf/abc.pdf
You can edit your apache config file and add a virtual host to that directory under a subdomain (say downloads)
After your edit, I am assuming you are using the file:// protocol, directly on the server. I would say just to remove the executable bit from your .pdf's file permissions. Run from a shell:
chmod -x /srv/www/for_html/abc.pdf

How can I setup Jekyll for a blog with a large image directory, so as to avoid duplicating that directory in the generated site?

I'm considering Jekyll for a site I'm putting together that will be a blog with lots of images (and other larg-ish media files). It's easy enough to to make a directory for images and then link to them as needed in the posts. But, as I understand it, when the site is generated, all the image data will be duplicated into the generated _site directory that holds the static files. Each time the site is generated the _site directory is emptied, and repopulated with the static version of the site.
Is there any way to, for example, drop a symlink to the images directory inside the site directory, and then maybe have jekyll ignore it when the static files are generated?
Or is there another way to go about this that makes more sense?
Assuming you are running on an apache web server, you can setup an Alias directive to serve images from a directory outside of the normal docroot. You need access to edit the VirtualHosts config or some other ability to create aliases directives (e.g. via a control panel).
For an example of how this would work, let's say you are storing your jekyll files under a directory called "/web/jekyll". To get your images directory do the following:
Add an "_images" directory along with your basic jekyll tree. Ending up with something like:
_config.yml
_images/
_layouts/
_posts/
_site/
index.md
Update your apache config to add the Alias directive like:
Alias /images /web/jekyll/_images
Reload the apache config and run jekyll to build the site.
Since the image directory name starts with an underscore, jekyll won't push/copy it to the output _site during the build. Apache will happily serve most files from your _site directory as normal, but when it sees something like "http://jekyll/images/test.jpg", instead of looking for the file under "/web/jekyll/_site/_images/test.jpg", it'll serve it from "/web/jekyll/_images/test.jpg".
Incidentally, I like a little more separation of the source content and output content than jekyll defaults to. So, I setup my directory structure as follows:
/web/jekyll/html/
/web/jekyll/images/
/web/jekyll/source/
/web/jekyll/source/_config.yml
/web/jekyll/source/_layouts
/web/jekyll/source/_posts
/web/jekyll/source/index.md
With the following option set in _config.yml
destination: ../html
And the apache alias directive setup with:
Alias /images /web/jekyll/images
Jekyll is run in the "/web/jekyll/source" directory, but output is sent to the "/web/jekyll/html" dir. Similar to the first example, calls to "http://jekyll/images/test.jpg" are served from "/web/jekyll/images/test.jpg". This setup doesn't really make a difference from a site serving perspective. I just like the cleaner separation between the raw source files, the fully baked output files and the images which work via the alias.
Correct, the first part of the jekyll command removes everything in the destination directory. The problem with that is the symlinks must be manually created again. So next, go ahead and create a script that does this each time.
Be sure that:
exclude: [jekyll, css, img] in the _config.yml file
linux: The ";" symbol runs first, second, third.. commands.
script: A file named jekyll with executable permissions containing
jekyll;
ln -s /var/www/css /var/www/_site/css;
ln -s /var/www/img /var/www/_site/img;
Finally run (./jekyll) that program instead of jekyll.
-Dan
Make a project page for the images.
Set up directory structure
/home/git/svnpenn.github.io
/home/git/img
Run Jekyll
# We cant add the symlink until after jekyll is done. We will remove the
# site folder and wait for it to rebuild.
rm -r _site
jekyll --server &
while [ ! -f _site/index.html ]
do
sleep 1
done
ln -s ../images _site/images
Note I was using this because I thought it would help publish time on GitHub
pages. It does not. GitHub can take 1-10 minutes to publish depending on the
server.
I know this has already been answered, but I went a slightly different route. I hosted all of my images in a public directory on Dropbox and use grunt to generate a manifest of the images. It keeps my repository small because the images don't get checked in. I detailed it a while back in a blog post.