Nginx as Reverse Proxy for Apache

Onlyme

Active member
Hi all,

Ive been reading on using Nginx as a reverse proxy for Apache and how i can get benefits from using both. I dot have much experience with nginx, so im hoping someone here with more experience can take a look at my configuration and let me know if its a good starting point/where it can be improved.

Code:
server {
    listen 80 default_server;
    # This is the document root
    root /var/www/html/;
    # This is the file which gets loaded by default. index.html gets loaded if there is no index.php
    index index.html index.htm index.php;
    # This has to be the domain you want to use
    server_name mysite.xyz;
    # Reverse Proxy
    location ~ \.php$ {
        include snippets/fastcgi-php.conf;
        fastcgi_pass unix:/var/run/php/php7.3-fpm.sock;
        proxy_pass https://127.0.0.1:444;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
    }
    # This configuration prevent the logger to log not found robots.txt
    location = /robots.txt {
        allow all;
        log_not_found off;
        access_log off;
    }
    # This tells the Nginx server to rewrite any requests which do not access a valid file to rewrite on to the index.php
    location / {
        try_files $uri $uri/ /index.php$is_args$args;
    }
    # This configuration prevent the logger to log not found favicon
    location = /favicon.ico {
        log_not_found off;
        access_log off;
    }
    # This says that all files with the given endings should be cached by the client
    location ~* .(jpg|jpeg|png|gif|ico|svg)$ {
        expires 365d; 
    }
    # .htaccess, .htpasswd, etc, will not be served. 
       location ~ /\.ht {
       deny all;
    }
    # hotlink protect your images and other file types
        location ~ .(gif|png|jpg|jpeg|svg|css|js|ico)$ {
        valid_referers none blocked mysite.xyz www.mysite.xyz;
        if ($invalid_referer) {
            return 403;
        }
    }
}
server {
       listen 443 ssl;
       # This is the document root
       root /var/www/html/;
       # This is the file which gets loaded by default. index.html gets loaded if there is no index.php
       index index.html index.htm index.php;
       # This has to be the domain you want to use
       server_name mysite.xyz;       
       ssl_certificate /etc/letsencrypt/live/mysite.xyz/fullchain.pem;
       ssl_certificate_key /etc/letsencrypt/live/mysite.xyz/privkey.pem;
       ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
       ssl_prefer_server_ciphers on;
       ssl_ciphers ECDHE-ECDSA-CHACHA20-POLY1305:ECDHE-RSA-CHACHA20-POLY1305:ECDHE-A:!DSS;    
       
    # Reverse Proxy
    location ~ \.php$ {
        include snippets/fastcgi-php.conf;
        fastcgi_pass unix:/var/run/php/php7.3-fpm.sock;
        proxy_pass https://127.0.0.1:444;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
    }
    # This configuration prevent the logger to log not found robots.txt
    location = /robots.txt {
        allow all;
        log_not_found off;
        access_log off;
    }
    # This tells the Nginx server to rewrite any requests which do not access a valid file to rewrite on to the index.php
    location / {
        try_files $uri $uri/ /index.php$is_args$args;
    }
    # This configuration prevent the logger to log not found favicon
    location = /favicon.ico {
        log_not_found off;
        access_log off;
    }
    # This says that all files with the given endings should be cached by the client
    location ~* .(jpg|jpeg|png|gif|ico|svg)$ {
                expires 365d;
    }
    # .htaccess, .htpasswd, etc, will not be served.
    location ~ /\.ht {
        deny all;
    }
    # hotlink protect your images and other file types
    location ~ .(gif|png|jpg|jpeg|svg|css|js|ico)$ {
        valid_referers none blocked mysite.xyz www.mysite.xyz;
        if ($invalid_referer) {
            return 403;
        }
    }
}

[code]
 
Back
Top Bottom