NGINX - Rewrite fancy url - apache

I thought rewrite in Nginx would be straight-forward. It looks like its not.
I would like some help from you guys. My previous setup was Apache2 + PHP. My new setup is Nginx + PHP-FPM.
I would like to transform a simple virtualhost from Apache2.
The directory structure is:
/api (this is root)
VERSION (1)
--- api.php
--- .htaccess
Fx. domain.tld/1/
The .htaccess files looks like this:
RewriteEngine on
RewriteRule ^([a-z]+)/([a-z]+)/api.json$ api.php?app=$1&sapp=$2 [L,QSA]
RewriteRule ^([a-z]+)/api.json$ api.php?app=$1 [L,QSA]
My new Nginx server file looks like this:
server {
listen 80;
root /var/www/domain.tld/api;
index index.php index.html;
server_name domain.tld;
location /1/ {
rewrite ^/([a-z]+)/([a-z]+)/api\.json$ /api.php?app=$1&sapp=$2;
rewrite ^/([a-z]+)/api\.json$ /api.php?app=$1;
}
location ~ \.php$ {
include php5;
}
}
But it does not works when I try to view: domain.tld/1/view/api.json or domain.tld/1/view/extended/api.json
Can anyone explain and give some examples to fix this?
Thanks in advance.

Take a look at how NGiNX processes location blocks, here: http://nginx.org/en/docs/http/request_processing.html
As you can see in your case the request is only served by your location /1/ block, as that is the only one of the two that matches. Problem is, NGiNX only uses one location block per request, so once it got into the location /1/ block, it will not go to the other location block later.
The solution here is to also include your php5 file in the location /1/ block at the end.
location /1/ {
rewrite ^/([a-z]+)/([a-z]+)/api\.json$ /api.php?app=$1&sapp=$2;
rewrite ^/([a-z]+)/api\.json$ /api.php?app=$1;
include php5;
}

Related

Apache to NGINX Rewrite Issue

I'm trying to convert this apache ReWriteRule to NGINX but it doesn't seem to be working.
Apache Rule: RewriteRule ^([A-Za-z0-9_-]+)\.php$ index.php?pagename=$1 [NC,QSA]
NGINX (not working): location / { rewrite ^/([A-Za-Z0-9_-]+)\.php? /index.php?pagename=$1; }
I do have the NGINX rule in a location block.
What am I missing here? I looked at the other similar questions but didn't find enough suitable clues to solve the problem. Thanks for the help.
Using converter:
server {
server_name example.com;
rewrite ^/([A-Za-z0-9_-]+)\.php$ /index.php?pagename=$1;
}
And indeed, many rewrites are easier with just a rewrite placed in the server {} context (no need for extraneous location).

nginx rewrite throw 404 with last and break

I have a virtual host like below for my Yii website
server {
root /var/www/code;
index index.php index.html;
server_name mydomain.com;
location / {
rewrite ^/en/keyword$ /en/spesfic_controller/ last;
....
}
}
if i use last or break in the above rewrite rule, nginx throws 404 error, but if i change it to permanent, it will redirect it and will work fine!! what might be wrong? or what i missed?
(nothing in error log)

Apache rewrite - Virtual dir to php script

Following the accepted answer to this post, I constructed a rewrite rule which is pretty straight forward, but doesn't seem to work as designed.
# /st doesn't exist, it's virtual
example.com/st/sometext
Rewrites to
# /app is a symlink to /app.php, and this link works if entered directly
example.com/app/st/sometext
And the rewrite rule:
RewriteRule ^st/(.*)$ app/st/$1 [L]
This rule is at the top of the rewrite block, after which there are other rules, but those rules shouldn't apply, due to the [L] constraint.
The rewrite log file has this:
(2) init rewrite engine with requested uri /st/sometext
(1) pass through /st/sometext
It doesn't seem like the rewrite rule is matching the url. What am I missing?
Ok, I resolved the issue by switching over to nginx (lol) and implementing this rewrite rule:
location /st {
rewrite ^/st/(.*)$ /app.php/st/$1 last;
}
The switchover to nginx really had little to do with the rewrite rule and more to do with getting away from apache 2.2 (default upstream version for centos), however with nginx, rewrite rules are more straight forward than apache's (imo).

HTTPS when SSL folder is present in root?

I want to force HTTPS for certain subdomains in nginx. I also do not want to edit the .conf file when i create a new subdomain that needs HTTPS. Is it possible to force HTTPS if an SSL folder is present in the root or something equivalent to it? So all i have to do to enable HTTPS on a new subdomain is add a new folder or something like that?
Here is my nginx.conf file:
#HTTP DOMAIN.COM
server {
listen 80;
server_name *.domain.com;
root /var/www/$http_host;
location /ssl {
rewrite ^ https://$http_host/$1 redirect;
}
edit:
I've been messing around a bit with symlinks but no luck yet. This is the new configuration I made:
server {
listen 80;
server_name *.domain.com;
root /var/www/$http_host;
location /var/www/ssl/$http_host {
rewrite https://$http_host/$1 redirect;
}
include error_page;
include location_php;
}
Am I coming close to my answer?
edit:
What I basically want is to be able to use ssl without having to change my .conf file.
All I need to do to enable HTTPS is change something somewhere around the docroot.
I don't think you can enable SSL based merely on the existence of a directory. But you can extend the configuration using file globbing:
include vhosts/*.conf
include vhosts/ssl/*.conf
Just drop a minimal file in the directory.
(note that you still need to set up certificates, most likely on a per vhost basis - and these should not be incide the document root).
I got it working! this is the config file:
server {
listen 80;
server_name *.domain.com;
root /var/www/$host;
if (-e /var/www/config/ssl/$host) {
rewrite ^ https://$host$request_uri? permanent;
}
include error_pages;
include php_config;
}
I still had to use an if statement but that's alright because it works now.

Amazon EC2 + 301 www redirection

I'm running a site on AmazonEC2 instance, and so far everything is ok, except a single glitch. When I navigate to
www.mysite.com/somepage.html
, browser happily opens the desired page. But when I try
mysite.com/somepage.html
, I go to the root of my site, i.e. to
www.mysite.com
LiveHTTPHeaders tells me that 301 redirection occurs. So here's the question: what configuration files do I need to change to make redirection respect URL path?
Also, I noticed that nginx does the redirection. Does it mean that redirection is happening outside of EC2 instance or not?
Thank you in advance.
You should have some kind of settings that is redirecting your request from http://mysite.com/ to http://www.mysite.com/
Following is kind of conf. settings you should find in your nginx.conf or inside /etc/nginx/sites-enabled/<site-vhost-name>
server {
server_name example.com;
rewrite ^/(.*) http://www.example.com/$1 permanent;
}
This tells NGINX to redirect request from http://mysite.com/ to http://www.mysite.com/.
If yes?, then blindly remove those two lines of conf.