I did some extensive searching and there are quite a few posts on nginx proxy_pass questions. I've tried modifying my problem to some of those questions with out any progress so here goes.
I'm in the midst of rewriting a php based website into Rails. The original site itself is simple 2 pages with forms. It uses a mod_rewrite / mod_proxy solution in Apache to mask the url of where the site continues after form post.
the php site has 3 directories that have nothing but 3 .htaccess files in them for simplicity sake lets call the directories a, b, c. each .htaccess file with the following
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule ^(.*)$ http://www.domainbeingpostedto.com/actual_letter_directory/$1 [P,L]
</IfModule>
I'm not an apache expert but I'm pretty sure the [P,L] is same as proxy_pass and last in nginx?
I'm trying to rewrite this solution for the php site that was converted into a rails cms using passenger and nginx instead.
The solution I have so far is not working because the rails app just returns a 404 with the page not found, so I know proxy_pass isn't forwarding the post request to the other server.
What I have for my nginx.conf file is:
server {
listen 80;
server_name newtestappdomain.com;
location /a/ {
proxy_pass http://www.domaintoacceptdatapostandbemasked.com/;
#rewrite ^/a/(.*)$ http://www.domaintoacceptdatapostandbemaskedcom/a/ last;
}
location /b/ {
proxy_pass http://www.domaintoacceptdatapostandbemasked.com/;
#rewrite ^/b/(.*)$ http://www.domaintoacceptdatapostandbemasked.com/b/ last;
}
location /c/ {
proxy_pass http://www.domaintoacceptdatapostandbemasked.com/;
#rewrite ^/c/(.*)$ http://www.domaintoacceptdatapostandbemasked.com/c/ last;
}
root /home/deploy/testapp/public; # <--- be sure to point to 'public'!
passenger_enabled on;
}
If I uncomment out the rewrite rule, it just bounces to the other site which i'm trying to mask. I did a header trace to verify as well. Didn't see anything post to the other domain. I'm kind of stumped as I'm really new to nginx and not sure what to do. Apache doesn't work well with rails and mod_rewrite / mod_proxy. Any insight would be great.
proxy_pass http://www.domaintoacceptdatapostandbemasked.com/;
This rule would be proxy requests to "/" location (leading slash in proxy_pass uri) (a/, b/ and c/ would be lost).
Just use uri without leading slash and it should work perfect
proxy_pass http://www.domaintoacceptdatapostandbemasked.com;
If you need to change uri, you can use rewrite before proxy_pass.
for example:
location /a/ {
rewrite /(a/.*)$ /actual_letter_directory/$1 break; # as from you .htaccess example
}
Related
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;
}
}
I'm attempting to test out NGINX and potentially make a switch from Apache. Which I've read nginx is much faster, but I want to be the judge of that. I'm having issues getting the configuration of NGINX to match that of my Apache setup -- mainly rewrite rules. I'll explain how my applications works and what I want to be able to do in NGINX.
Currently I have my application handling all the REQUEST_URI's sent to the server. Even if the URI doesn't exist, my application handles processing of that URI. I'm able to do this because of a rewrite rule for Apache.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?_url=$1 [QSA,NC]
</IfModule>
As you can see if the file or directory doesn't actually exist, it gets sent to index.php. I don't even check for the query string, I just handle the processing of the URI itself via the PHP variable $_SERVER['REQUEST_URI'] which is setup inside NGINX as fastcgi_param REQUEST_URI $request_uri;. I want to accomplish this exact thing with NGINX, but I've been only kind of successful with that.
So basically, if domain.com/register.php exists, then it is going to go to that url, if not it is going to be redirected to domain.com/index.php and the application handles the URI from there.
Here is my config file for my server. This is included at the bottom of nginx.conf file
server {
listen ####:80;
server_name ####;
charset utf-8;
access_log /var/www/#####/logs/access-nginx.log;
error_log /var/www/#####/logs/error-nginx.log;
root /var/www/######/public/;
location / {
index index.php index.html;
include /etc/nginx/mime.types;
try_files $uri /index.php?_url=$1;
include /etc/nginx/fastcgi.conf;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_index index.php;
fastcgi_pass unix:/var/run/php-fpm.socket;
autoindex on;
autoindex_exact_size off;
autoindex_localtime on;
rewrite_log on;
}
}
So this kind of works. What I mean by that is the try_files $uri /index.php?_url=$1 directive is processing the URI the way I want it to, but MIME types don't seem to work. Everything is being processed as text/html. Which means that my .css and .js files have to be turned into a .php file and a header attached in order for it to be processed correctly. image and font files seem to function correctly, but Chrome still shows the mime type as html. I have the mime.types file being included, so I can't figure out why it's doing that. I did try to use a "rewrite" directive to handle the what try_files is doing, but that didn't work.
This is the rewrite I tried inside the location / block:
if (!-e $request_filename){
rewrite ^(.*)$ /index.php?_url=$1;
}
So my question is this: How do I properly rewrite my uri for non-existent files and directories while serving the proper mime type for the files automatically?
I ended up solving my own issue. What I had to do here was to process PHP files on their own and it took for a while to figure out that for sure. Here is the final .conf file that sends the correct mime types and also rewrites the way I needed it. Hopefully this helps someone else as well.
server {
listen #######:80;
server_name ######;
charset utf-8;
access_log /var/www/######/logs/access-nginx.log;
error_log /var/www/#######/logs/error-nginx.log;
root /var/www/#########/public/;
location ~ \.php$ {
include /etc/nginx/mime.types;
try_files $uri /index.php?_url=$1;
include /etc/nginx/fastcgi.conf;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_index index.php;
fastcgi_pass unix:/var/run/php-fpm.socket;
}
location / {
index index.php index.html;
include /etc/nginx/mime.types;
try_files $uri /index.php?_url=$1;
autoindex on;
autoindex_exact_size off;
autoindex_localtime on;
rewrite_log on;
}
}
Using the location ~ .php$ section made it so that only PHP files were being sent to php-fpm. I also use the try_files directive to handle sending all the URI's that don't exist to my script, which is what my application expects. Hopefully this helps someone else out there!
My site is running on Nginx and I am trying to add a software in the sub-directory of the site that uses Apache's mod_rewrite rules. E.g. www.mydomain.com/mySubfolder
Here is the Apache .htaccess
#Options -Indexes
<ifModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_URI} ^/system.*
RewriteRule ^(.*)$ index.php?/$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)$ index.php?/$1 [L]
</ifModule>
So far I managed to get the main page to work but when requesting the login page, it is causing a URL redirect loop. E.g. www.myDomain.com/login
With this:
location /mySubfolder {
if (!-e $request_filename) {
rewrite ^(.*)$ /mySubfolder/index.php?q=$1 last;
break;
}
}
I have been reading and trying to learn how to convert Apache to Nginx and even used the .htaccess to Nginx converter I found at http://winginx.com/htaccess but the tool doesn't seem to recognize the %{REQUEST_URI} ^/system.* part. Upon my research and study, I came up with:
location /mySubfolder {
if ($request_uri ~ "^/(system.*)$") {
rewrite ^/(.*)$ index.php?/$1 last;
}
if (!-e $request_filename) {
rewrite ^(.+)$ /mySubfolder/index.php?q=$1 last;
break;
}
}
I am a complete noob at this and was even wondering if I am even close to accomplish this conversion to work. Please help.
Thank you.
was even wondering if I am even close to accomplish this conversion to
work
You've basically taken the wrong approach to use in Nginx.
Although it is kind of natural to assume that the rewrite rules in Apache would be mapped to the rewrite in Nginx, they aren't. Instead they are mostly mapped to the location rules.
Your block for mySubfolder should look like:
location ^/mySubfolder {
try_files /mySubfolder/index.php?$args =404;
}
You aren't actually rewriting anything - you are just telling nginx that any requests that start with /MySubfolder should be served by the files listed in try_files. btw you have to tell Nginx to pass the query string through which is what the args is doing.
You can append the original URL (i think) though it may be easier just to use $_SERVER['REQUEST_URI'] inside your script.
I believe the rewrite rule you have to the same start URI is causing the /mySubFolder to keep matching.
In Nginx rewriting is only used when you want to make external URL paths be served by different internal urls, and normally the rewrite rule is not inside a location block.
For example I have a file server, that serves up images and other files. I have these rewrite rules in the server block:
rewrite ^/image/(\d+)/(\w+)/(.+)\.([^\.]*)$ /proxy/proxyImage.php?typeID=$1&mode=$2&imagePath=$3.$4&resizeAllowed=TRUE&type=image last;
rewrite ^/image/(\d+)/(.+)\.([^\.]*)$ /proxy/proxyImage.php?typeID=$1&imagePath=$2.$3&resizeAllowed=TRUE last;
rewrite ^/file/(\d+)/(.+)\.([^\.]*)$ /proxy/proxyFile.php?typeID=$1&imagePath=$2.$3&resizeAllowed=FALSE last;
to make the external URLs look nice. But then they are all served by the location block:
location ~* ^/proxy {
try_files $uri =404;
fastcgi_pass unix:/opt/local/var/run/php54/php-fpm-images.sock;
include /documents/projects/intahwebz/intahwebz/conf/fastcgi.conf;
}
The location block doesn't need to know about the URL rewrites because they are done before the location block is encountered.
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.