damir
By:
damir

How do I Rewrite URL's in NGINX server block?

September 19, 2014 18.4k views

Hi,

I have bought a SSL certificate and after many trials and errors I have succeded in installing the SSL. Now, I want to rewrite the URL's:

From To
http://wknet.se https://www.wknet.se
http://wknet.se/index.php https://www.wknet.se
http://www.wknet.se/index.php https://www.wknet.se
wknet.se https://www.wknet.se
wknet.se/index.php https://www.wknet.se
www.wknet.se/index.php https://www.wknet.se

I have googled and looked in this community and I have tested
rewrite ^ https://$server_name$request_uri permanent;
but my website gets in a redirect error loop.

I have now commented it out.

Please can someone tell me how I do those rewrites?

4 Answers

As mentioned in Nginx pitfalls, rewrite regexes can slow things down. To redirect http traffic to https, you should use a 301 Moved Permanently redirect instead:

server {
    listen         80;
    return 301 https://www.wknet.se$request_uri;
}

server {
    listen         443;
    server_name www.wknet.se;
    ...

I still have some issues with my server block:

server   {

   server_name www.chowchowbath.com;
   root /var/www/ghost/;
   rewrite  ^/(.*)$  http://chowchowbath.com/$1 permanent;

   }  

server {
    listen 80;
 server_name  chowchowbath.com;

    root /var/www/ghost/;

   rewrite ^/2006/12/01/the-year-you-were-born-part-one/ http://www.thebangaloreblog.com/2006/11/the-year-you-were-born-part-one.html permanent;    
  }

so everything redirects from www to non www in the first server block. additionally in the second block, the url specified also redirects to the bangalore blog. but however anything else i had on http://chowchowbath.com/<blog-post-url> says 404 page and does not show up whats there.
What am i missing here?

  • You also need to configure nginx to pass the requests to Ghost:

    server   {
    
       server_name www.chowchowbath.com;
       root /var/www/ghost/;
       rewrite  ^/(.*)$  http://chowchowbath.com/$1 permanent;
    
       }  
    
    server {
        listen 80;
     server_name  chowchowbath.com;
    
        root /var/www/ghost/;
    
       rewrite ^/2006/12/01/the-year-you-were-born-part-one/ http://www.thebangaloreblog.com/2006/11/the-year-you-were-born-part-one.html permanent;    
    
        location / {
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header HOST $http_host;
            proxy_set_header X-NginX-Proxy true;
    
            proxy_pass http://127.0.0.1:8080;
            proxy_redirect off;
        }
      }
    

    Replace 8080 with the port that Ghost is listening on and restart nginx.

  • How do I know which port ghost is listening onto? is there a way to find that out?

kamlan7, many thanks for your suggestion. it works perfectly as I expected now. I have one more query to wrap up this discussion. So I have had this situation where this is what i wanted to achieve.

url1/post1 -> url2/postx
url1/post2 -> url2/posty

this is now achieved by your above suggested code changes.
further i have already put in www.url1 -> url1 redirection as well.

now with all this in place, I want to now do this:

if someone types just http://url1 , i want it to redirect to http://url2 , without affecting any of the previously suggested changes and those things that work with them.

I hope you understood my query. (in simple english i want entire domain url1 to point to url2 at very top level, but still if someone searches or goes to particular url on url1, the redirect to specific url2 must still work)

Is this possible in your opinion?

many thanks again.
Srikanth

trying to when change /client/BigBlueButton.html portion of the URL according to - http://docs.bigbluebutton.org/support/faq.html#how-do-i-change-the-client-bigbluebutton-html-portion-of-the-url,

but getting -
``` 404 Not Found
code

my /etc/bigbluebutton/nginx/client.nginx:
location /client/BigBlueButton.html { root /home/firstuser/dev/bigbluebutton/bigbluebutton-client; index index.html index.htm; expires 1m; BigBlueButton Flash client.

location /client {
root /home/firstuser/dev/bigbluebutton/bigbluebutton-client;
index index.html index.htm;
}
code

my /etc/bigbluebutton/nginx/rewrite.nginx:

location /client/BigBlueButton.html {
rewrite ^ /conference permanent;
}

location /conference {
alias /var/www/bigbluebutton/client;
index BigBlueButton.html;
expires 1m;
}
code

$sudo /etc/init.d/nginx restart

/var/log/nginx/bigbluebutton.access.log

33.126.263.65 - - [16/Aug/2015:12:37:57 -0400] "GET / HTTP/1.1" 200 2852 "-" "Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/44.0.2403.155 Safari/537.36"
33.126.263.65 - - [16/Aug/2015:12:37:57 -0400] "GET /css/bijou.min.css HTTP/1.1" 200 2753 "http://M_IP_Ad/" "Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/44.0.2403.155 Safari/537.36"
33.126.263.65 - - [16/Aug/2015:12:37:57 -0400] "GET /css/style.css HTTP/1.1" 200 2918 "http://M_IP_Ad/" "Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/44.0.2403.155 Safari/537.36"
33.126.263.65 - - [16/Aug/2015:12:37:57 -0400] "GET /css/font-awesome.min.css HTTP/1.1" 200 20766 "http://M_IP_Ad/" "Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/44.0.2403.155 Safari/537.36"
33.126.263.65 - - [16/Aug/2015:12:37:57 -0400] "GET /images/jimtalk-logo.png HTTP/1.1" 200 10251 "http://M_IP_Ad/" "Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/44.0.2403.155 Safari/537.36"
33.126.263.65 - - [16/Aug/2015:12:37:57 -0400] "GET /images/bbb-setup-audio.jpg HTTP/1.1" 200 18876 "http://M_IP_Ad/" "Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/44.0.2403.155 Safari/537.36"
33.126.263.65 - - [16/Aug/2015:12:37:57 -0400] "GET /images/bbb-viewer-overview.jpg HTTP/1.1" 200 21929 "http://M_IP_Ad/" "Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/44.0.2403.155 Safari/537.36"
33.126.263.65 - - [16/Aug/2015:12:37:57 -0400] "GET /images/bbb-presenter-overview.jpg HTTP/1.1" 200 18309 "http://M_IP_Ad/" "Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/44.0.2403.155 Safari/537.36"
33.126.263.65 - - [16/Aug/2015:12:37:58 -0400] "GET /fonts/fontawesome-webfont.woff?v=4.1.0 HTTP/1.1" 200 83760 "http://M_IP_Ad/css/font-awesome.min.css" "Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/44.0.2403.155 Safari/537.36"
MIPAd - - [16/Aug/2015:12:38:00 -0400] "POST /bigbluebutton/api/create?name=Demo+Meeting&meetingID=Demo+Meeting&voiceBridge=72274&attendeePW=ap&moderatorPW=mp&record=false&checksum=a93ab8433532c633ab2467afc0d91e0eb1dc4e88 HTTP/1.1" 200 488 "-" "Java/1.7.079"
33.126.263.65 - - [16/Aug/2015:12:38:00 -0400] "GET /demo/demo1.jsp?username=%D7%99%D7%A2%D7%99%D7%A2%D7%99&action=create HTTP/1.1" 200 1003 "http://M
IPAd/" "Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/44.0.2403.155 Safari/537.36"
33.126.263.65 - - [16/Aug/2015:12:38:01 -0400] "GET /bigbluebutton/api/join?meetingID=Demo+Meeting&fullName=%D7%99%D7%A2%D7%99%D7%A2%D7%99&password=mp&checksum=3438ed39a50723be59798038f86fcba0af30b325 HTTP/1.1" 302 0 "http://M
IPAd/demo/demo1.jsp?username=%D7%99%D7%A2%D7%99%D7%A2%D7%99&action=create" "Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/44.0.2403.155 Safari/537.36"
33.126.263.65 - - [16/Aug/2015:12:38:02 -0400] "GET /client/BigBlueButton.html HTTP/1.1" 301 193 "http://M
IPAd/demo/demo1.jsp?username=%D7%99%D7%A2%D7%99%D7%A2%D7%99&action=create" "Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/44.0.2403.155 Safari/537.36"
33.126.263.65 - - [16/Aug/2015:12:38:02 -0400] "GET /conference HTTP/1.1" 301 193 "http://M
IPAd/demo/demo1.jsp?username=%D7%99%D7%A2%D7%99%D7%A2%D7%99&action=create" "Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/44.0.2403.155 Safari/537.36"
33.126.263.65 - - [16/Aug/2015:12:38:02 -0400] "GET /conference/ HTTP/1.1" 403 208 "http://M
IP_Ad/demo/demo1.jsp?username=%D7%99%D7%A2%D7%99%D7%A2%D7%99&action=create" "Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/44.0.2403.155 Safari/537.36"
code

Thank you
Have another answer? Share your knowledge.