I deployed an Angular2 application on my Apache web server and with the following .htaccess,
<IfModule mod_rewrite.c>
Options +FollowSymlinks
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]
RewriteRule ^ index.html [L]
</IfModule>
and an additional configuration in index.html,
<base href="/applicationName/">
the application was able to load without any issue and there was no redirection problem.
I'm now trying to setup the same application in my Nginx server but I couldn't seem to make it work.
I understand that there is no .htaccess in Nginx, how do I convert the above .htaccess to work in Nginx main configuration?
Thanks a lot!
Nginx is actually quite a lot easier than apache in my opinion. One way to do it is to create a server block like this. This is of course if you have access to the configuration file of nginx on your server.
server {
listen 80;
server_name example.com;
root /path/to/your/index;
index index.html;
location / {
try_files $uri $uri/ /index.html;
}
}
Related
I am attempting to deploy a multipage vue3 frontend to an application. It works fine in development mode, but in production, when I attempt to navigate to a url via the search bar -- e.g. https://mywebsite.com/customers, I get a 404.
I am able to fix this by redirecting to the homepage using the suggestion on vue-router's documentation, but then it just goes to the homepage rather than showing the actual page I would like to visit. Is there some way around this that I am missing? An additional step or does this suggest I am misconfiguring this somehow?
I solved this by setting up the server. You need to understand what server you have installed. Go to config update and restart the server.
Here are the Server Configurations
Apache
<IfModule mod_negotiation.c>
Options -MultiViews
</IfModule>
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.html$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.html [L]
</IfModule>
nginx
location / {
try_files $uri $uri/ /index.html;
}
I'm new to nginx, but I'm trying to convert
# Disallows file browsing
Options -Indexes
# Custom Error Pages
ErrorDocument 403 /error-docs/403.php
ErrorDocument 404 /error-docs/404.php
ErrorDocument 500 /error-docs/500.php
# Turn on the Apache Rewrite Engine
RewriteEngine On
#Conditional to see if it is a real file or directory
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# If not then pass the query string to index.php and stop any further rules
RewriteRule (.*) index.php/$1 [PT,L]
to nginx syntax. I've tried the suggestion at Routing requests through index.php with nginx but it doesn't do quite what I need it to do. I've also tried the auto converters http://winginx.com/en/htaccess and http://www.anilcetin.com/ but the rules they output don't work for me either. Any help is appreciated.
The issue I had was actually related to my PHP and MySQL code.
location / {
try_files $uri $uri/ /index.php$args;
}
works fine. Thanks for the link.
I am trying to change the configuration on my new Nginx server so it matches my current Apache settings.
At the moment I am using this htaccess file:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*) index.php?url=$1 [L,QSA]
I have found a converter that could "translate" this into the Nginx equivalent. I have tried the following:
location / {
if (!-e $request_filename){
rewrite ^/(.*) /index.php?url=$1 break;
}
try_files $uri $uri/ =404;
}
But when I try to set a url query like this http://domain.tld/something my php file gets returned and downloaded and that ain't supposed to happen.
What I expect to happen is when a url like http://domain.tld/something is entered it gets treated like http://domain.tld/index.php?url=something
Can someone tell me what I am doing wrong?
Unlike Apache, nginx doesn't come out-of-the-box ready to run PHP. You need to setup a handler to deal with php files, otherwise nginx will serve them up just like any regular file.
See: Nginx downloads php instead of running it
Also: review the NGINX documentation on using fast-cgi to run php.
Background:
I want to setup Bugify on my Ubuntu Server running nginx. I followed the instructions, installed the dependencies and the installation was successful. Once I enter my licence key and click on "Install Bugify" it's redirecting to http://dev.mysite.com/login?new and the only thing I'm seeing is 404 Not Found.
I know that nginx isn't officially supported but according to the support forum it should be possible to run it.
Question:
There's a .htaccess file with rewrite rules in the webapp's public directory and I guess the problem causing the 404 error is that nginx isn't able to work with the .htaccess file, so I'll have to convert the rewrite rules to the nginx syntax.
I'm not really familiar with apache's rewrite rules so I'd need some help figuring this out.
The content of the .htaccess file is:
# Rewriting
RewriteEngine On
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteRule ^.*$ index.php [NC,L]
I was using this tool to convert the rules but it's having some troubles with the - flags.
#ignored: "-" thing used or unknown variable in regex/rew
Thanks in advance!
Bugify uses the Zend Framework, so the rewrite rules for ZF should work for Bugify also. I have copied the suggested nginx config below from http://wiki.nginx.org/Zend_Framework
server {
listen 80;
server_name www.example.com;
root /var/www/www.example.com/myapplication;
index index.html index.htm index.php;
location / {
# This is cool because no php is touched for static content
try_files $uri $uri/ /index.php;
}
location ~ \.php$ {
fastcgi_pass unix:/usr/local/zend/tmp/php-fastcgi.socket;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /var/www/www.example.com/myapplication$fastcgi_script_name;
include fastcgi_params;
}
}
I'm stuck with this, my skills in the web servers area are poor...
I have an Nginx acting as a proxy for an Apache2 running with mod_wsgi and mod_rewrite. What I want to do is rewrite every URL from www.example.com to example.com, i.e. stripping the www part from each URL request before serving. This is the layout of the different conf files:
=== /etc/nginx/sites-enabled/example.com ===:
http://dpaste.com/82638/
=== /etc/apache2/sites-enabled/example.com ===:
http://dpaste.com/hold/82645/
=== /home/nabuco/public_html/example.com/example/apache/example.wsgi ===:
http://dpaste.com/82643/
In my old set up I had an Apache2 running mod_python, and the only thing I had to do was putting an .htaccess file like this:
Options -Indexes
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\.example\.com$ [NC]
RewriteRule ^(.*)$ http://example.com/$1 [R=301,L]
That worked perfectly.
But I tried putting the same .htaccess file into /home/nabuco/public_html/nomadblue.com/nomadblue/apache/.htaccess. If I cast a request without leading www, such as http://example.com/ or http://example.com/whatever, everything goes well. However, if I try the www version of http://www.example.com/ I am redirected to:
http://example.com/example.wsgi/
Do I have to run rewriting rules from nginx instead? I tried that too, adding this to the nginx conf file:
rewrite ^/(.*) http://example.com/$1 permanent;
but now I am getting what firefox calls a "circular loop"...
So who can I get this (I guess trivial) thing up?
Thanks in advance,
Hector
The easiest is to rewrite with nginx. Put that rewrite rule in a dedicated "server" bound to www.example.com
server {
listen 80;
server_name www.example.com;
rewrute ^/(.*) http://example.com/$1 permanent;
}
All right I found the solution to avoid the circular loop... by creating TWO server sections in my nginx config file, one for www.example.com -- which has the rewrite rule suggested by rzab -- and the other for example.com, which contains all the rest of directives.