Other solutions, instead of .htaccess for NGINX - performance

I'm working on a larger Web based Project, that probably will have to handle a couple of hundred requests per minute in a later stage (or more).
I never worked with NGINX, but reading the comparisions with apache, it seems that I probably should go the NGINX route. Reading about it, I always see that '.htaccess' files are a lazy solution. Now, I would like to avoid relying on .htaccess files, but my question is how?
What I mean is - if .htaccess are considered the lazy, hacky solution, what exactly is the clean solution for this issues:
Redirecting all specific URL's (except for CSS / Images, and this stuff), to a single php file (by the way, does that have a performance impact, if a single file like index.php handles the URL's and includes the required templates? Should I avoid that, and split into dozens of files?)
Reading PHP Tags inside of CSS Files ("AddHandler application/x-httpd-php .css ...")
Another layer of login (.htaccess authentication, with that little popup window)
This are basically the three things I'm using .htaccess for. Especially the first one, basically makes the whole Application work at all.
But for this Project, I'm really trying to stay clean, and up-to-date, solution wise. What are my possibilities? Where could I increase my performance? And how can I solve the 3 mentioned problems, whitout using a .htaccess, to develop on NGINX?
Thank you very much for your time and effort.

In order to get the most out of nginx, you have to understand that it is not a web server (like Apache), but a proxy. In simple terms, it acts like a giant rule engine to pass stuff along to other applications based on pattern matching on requests.
So to run PHP with nginx, you need a separate server (or process) that runs PHP. This can be Apache, but PHP includes a FastCGI Process Manager (FPM).
Here is basic boilerplate to get you going:
server {
listen 80;
server_name www.example.com example.com;
access_log /var/www/www.example.com/logs/access.log;
error_log /var/www/www.example.com/logs/error.log;
root /var/www/www.example.com/public_html;
location / {
index index.html index.htm index.php;
auth_basic "Username Required";
auth_basic_user_file /etc/nginx/htpasswd;
}
location ~ (\.php|\.css)$ {
try_files $uri =404;
include /etc/nginx/fastcgi_params;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_pass php;
}
}

Question is offtopic, but to help you:
http://nginx.org/en/docs/http/converting_rewrite_rules.html
http://www.anilcetin.com/convert-apache-htaccess-to-nginx/

Related

How to redirect example.com and example.com/anything to example.com/blog

I want to redirect example.com and example.com/anything to example.com/blog. Please note few things.
I refer example.com for a 1 domain.
I use apache as web server.
My document root is set to /var/www/html/public within apache vhost conf file (For a laravel APP).
I tried setting redirects in .htaccess and using apache vhost conf file and I get redirect too many times error.
Can someone help me to accomplish this please?
This probably is what you are looking for: rewriting on the level of the http server:
RewriteEngine on
RewriteRule ^/?$ /blog [R=301]
RewriteRule ^/?anything/?$ /blog [R=301]
If by "anything" you actually mean anything so that a redirection should get applied regardless of the requested path, then this should do:
RewriteEngine on
RewriteCond %{REQUEST_URI} !/blog
RewriteRule ^ /blog [R=301]
It is a good idea to start out with a 302 temporary redirection and only change that to a 301 permanent redirection later, once you are certain everything is correctly set up. That prevents caching issues while trying things out...
This implementation will work likewise in the http servers host configuration or inside a distributed configuration file (".htaccess" file). Obviously the rewriting module needs to be loaded inside the http server and enabled in the http host. In case you use a distributed configuration file you need to take care that it's interpretation is enabled at all in the host configuration and that it is located in the host's DOCUMENT_ROOT folder.
And a general remark: you should always prefer to place such rules in the http servers host configuration instead of using distributed configuration files (".htaccess"). Those distributed configuration files add complexity, are often a cause of unexpected behavior, hard to debug and they really slow down the http server. They are only provided as a last option for situations where you do not have access to the real http servers host configuration (read: really cheap service providers) or for applications insisting on writing their own rules (which is an obvious security nightmare).
You can do that in your routes
// web.php
Route::redirect('/', '/blog');
Route::redirect('/anything', '/blog');

Laravel ISPConfig/Nginx directives

I have a laravel app and my client needs to access it via subfolder (let's say clientdomain.com/laravelapp/). I have already moved the contents of laravel's /public/ folder to /laravelapp and setup the index.php file so it references the right path.
I ended up with the following structure:
- /public_html (webroot of the server)
- /myapp/ (this is the public folder that was inside the laravel project)
- /laravel-framework (this is the laravel project folder with controllers, views, vendors, etc)
It works ok when I access the main route (clientdomain.com/laravelapp), it shows views, images and everything ok. The problem is when I try to access a different route, it shows 404.
I can't manually setup the nginx conf file, but ISPConfig has a box for nginx directives.
What are the right directives for a setup like mine?
I thank you in advance.
Try to add this to box for nginx directives.
location / {
try_files $uri $uri/ /index.php?$args;
}

How can I redirect non www http to https www in web.config

I want to redirect all of the following types of requests to https://www.example.com
http://example.com
http://www.example.com
https://example.com
This depends on how you want to redirect. You could...
Redirect from the client side. This is easy to do, simply add a script tag in the <head> of the page HTML document that contains the following.
let excluded = ['http://www.example.com', 'https://example.com', 'http://example.com']
if (excluded.indexOf(location.origin) !== -1) {
location.href = 'https://www.example.com'
}
There is only one problem with this code, being that the browser might complain about too many redirects. The second option is safer.
If you know how, you could use .htaccess. You will find this file in the root directory of your web server. Add the following lines:
RewriteEngine On
RewriteCond %{HTTPS} !on
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}
(Check out this question for more on this.)
If the page is not strictly static, you could redirect from the backend. There are a number of ways to do this, the most likely being to just use an npm module such as forcedomain (my personal favorite) to redirect when the request is made, rather than when the page loads. This is more efficient and the browser will not get cranky on you.
Use multiple CNAME records. If you aren't familiar with DNS, I would disregard this part of the answer. Basically, make a CNAME where the host is #, and the value is www. This will get all requests to the apex domain (example.com) and redirect them to the subdomain (www.example.com). When it comes to the protocol issue, there are a few ways of doing this, the best option being wildcard redirects (if your registrar provides this).
As a general rule of thumb, it's best to use DNS records, the .htaccess file, or some sort of backend plugin if possible for redirects if you know how. This all depends on your hosting, the nature of your website (static or dynamic), and your level of knowledge on these sorts of things.
Recommend using Firebase Hosting: https://firebase.google.com/docs/hosting/quickstart

Rewrite numbers to default page

I am trying to rewrite a condition that can take any page that has numbers.html, lets say the like the following:
12345.html
33443.html
234545656.html
9797423.html
and just redirect it to index.php
RewriteRule ^([0-9]+)$ index.php [L]
I tried the following but it doesn't work, any help is very much appreciated.
Two immediate issues with your code:
you have to switch on the rewriting engine prior to using it and
if you want to match file names ending with .html, then you have to code that.
Have a try with this:
RewriteEngine on
RewriteRule ^([0-9]+)\.html$ index.php [L]
Then a few more hints:
the code you tried will only work in .htaccess style files. But the usage of such files has to be enabled first in your http server configuration.
a .htaccess style file has to be placed at the correct location, in this example within the folder actually holding the index.php file.
the rewriting module has to be installed and enabled, without those commands will not be available.
And a general remark:
Those .htaccess style files are notoriously error prone, hard to debug and they really slow down the http server, often for nothing. You should always prefer to place such commands inside the real host configuration of your http server. .htaccess style files are only offered as a last option for situations where you have no access to that configuration, for example when using a really cheap shared hosting provider.

HTTP Protocol: How does one figure out where a site's homepage is located

I can't be the only person to have ever wondered this, but I couldn't find the question answered anywhere else on the site. I've successfully requested specific files from sites by sending HTTP requests (in ruby with sockets). I don't know, however, how to find out what file a site's homepage is located in to start with. I think this question is fairly clear, but just in case:
#!/usr/bin/ruby
require 'socket'
s = TCPSocket.new("www.example.edu", 80)
s.print("GET /index.html HTTP/1.0\r\n\r\n")
while (t = s.recv(50))
print t
end
exit 0
That's what does work, but what I would like to do is get a homepage without knowing its file name beforehand. Anyone know? Thanks in advance!
It works the other way around: you request just / (i.e. GET / HTTP/1.1) and then the webserver decides to lookup index.html. Or any other document/script/whatever that has been configured.
This often happens to be index.html, but is not necessarily so. Often it will be index.php or similar.
My default apache configuration says:
DirectoryIndex index.html index.cgi index.pl index.php index.xhtml index.htm
I could put anything in there.

Resources