If I install nginx as proxy on my Debian wheezy server, what steps would be needed to change my apache configuration so all static content is directly delivered by nginx?

I already have some 250 domains running on my server and thought about re-routing everything through nginx to get past the problem, that apache2.2 is not able to use a DH-cypher stronger than 1024 to be save against logjam.


If you want to have Apache behind Nginx as proxy and static content server I see 2 solutions:

1. You can create new sub-domain just for static content, e.g. static.yoursite.asd . It will of course require you to modify your DNS record(here is how to do this http://content.websitegear.com/article/subdomain_setup.htm). In Nginx config file you will need to have 2 server blocks. One for serving your static content and one for redirecting traffic to Apache. Here is how your server block for static content may look like:

server {
    listen 80;

    server_name static.localhost;

    location / {
        root /path/to/static/content;
        # 404 if file does not exist
        try_files $uri $uri/ =404;

And here is example server block for redirecting traffic to Apache: (let's presume that your Apache works on port 8000)

server {
    listen 80 default_server;
    listen [::]:80 default_server;

    server_name _;

    location / {
        proxy_pass http://localhost:8000;

2. You can have one server block on Nginx, but use two different locations. It costs less effort, because you don't have to modify DNS records. Let's say that you want to have your static files on location yoursite.asd/static/ and redirection to Apache on root location( yoursite.asd/ ).

Here is example location for serving static content:

location /static/ {
        root /path/to/content;
        # we don't want to have static files only in folder "static" in 
        # root document, so let's rewrite it to our root document
        rewrite ^/static/(.*)$ /$1 break;

And here is example location for redirecting traffic to Apache on port 8000:

location / {
        proxy_pass http://localhost:8000;
  • cant I just simply redirect everything through nginx? with your suggestion, woudn't I hae to change all webpages on my server, so they link all static content under another URL? that would be impossible to achieve with all 250 domains on my server – rubo77 May 30 '15 at 23:03
  • Yes, you would have to change your URLs. Redirecting everything to Apache will also work. You can use "proxy_pass localhost:8000;" in your root location and use one server block. – Jakski May 30 '15 at 23:10
  • Alternatively you can only redirect non-static links to Apache, if they end with some particular extension, e.g. *.php. It can be achieved with regex in location block, for example "location ~ \.php$" – Jakski May 30 '15 at 23:22
  • Would I have to convert all my sites- available from Apache format into the nginx format? – rubo77 May 30 '15 at 23:33
  • 1
    If you are using Debian, you should have example redirection of URLs ending with *.php to fastCGI in your config file of Nginx. You can use this location block, but change it to proxy_pass to your Apache and delete lines related to fastCGI. Remember though that aliases doesn't have to contain *.php extension even if you are using PHP, so this solution is impractical if your Apache uses them. I would just redirect everything to Apache because previous solution is very limiting. – Jakski May 31 '15 at 0:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.