I am trying to setup Nginx as a reverse proxy and webserver together. and I'm having issues in trying to understand how I can do it.
Let's assume I am using the default Symfony2 nginx configuration
(http://symfony.com/doc/current/cookbook/configuration/web_server_configuration.html):
server {
server_name example.com www.example.com;
root /var/www/project/web;
location / {
# try to serve file directly, fallback to app.php
try_files $uri /app.php$is_args$args;
}
# DEV
# This rule should only be placed on your development environment
# In production, don't include this and don't deploy app_dev.php or config.php
location ~ ^/(app_dev|config)\.php(/|$) {
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_split_path_info ^(.+\.php)(/.*)$;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param HTTPS off;
}
# PROD
location ~ ^/app\.php(/|$) {
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_split_path_info ^(.+\.php)(/.*)$;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param HTTPS off;
# Prevents URIs that include the front controller. This will 404:
# http://domain.tld/app.php/some-path
# Remove the internal directive to allow URIs like this
internal;
}
error_log /var/log/nginx/project_error.log;
access_log /var/log/nginx/project_access.log;
}
I want to add a configuration so the Nginx will also function as a reverse proxy. and not only a webserver.
-
Do I need to add to the same configuration file the
proxy_pass
,proxy_cache
, etc.. configurations? -
Do I need to set a configuration for a specific routes? or to disable them?
-
If for example if I don't want the route
/app_dev.php/abc
to be cached? what do I need to do?
Best Answer
Basically nginx is proxy server. Its capabilities include proxy HTTP, HTTPS, IMAP, POP3, SMTP and other protocols. For HTTP(S) proxy, the backend can be either FastCGI server like PHP-FPM or another web server.
For FastCGI backend like you need fastcgi module. For example, you need to define the backend with
fastcgi_pass
. For proxying another website, you need HTTP proxy module. You need to use direction likeproxy_pass
,proxy_cache
to control the behaviour of this module.YES
For example, you need proxy the the specific URL
www.example.com/myawesomeapp
, then use location to match the URLUse proxy_cache_bypass. You can set by
if
directive like this tutorial.