使用Nginx作为Web服务器和反向代理

我正在尝试将Nginx设置为反向代理和Web服务器。 我在试图了解如何做到这一点上遇到了问题。

假设我使用了默认的Symfony2 nginxconfiguration( 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; } 

我想添加一个configuration,所以Nginx也将作为一个反向代理。 而不仅仅是一个networking服务器。

  1. 我需要添加到相同的configuration文件的proxy_passproxy_cache等..configuration?

  2. 我是否需要为特定路线设置configuration? 或禁用它们?

  3. 如果例如,如果我不想要的path/app_dev.php/abc被caching? 我需要做什么?

基本上nginx是代理服务器。 其function包括代理HTTP,HTTPS,IMAP,POP3,SMTP和其他协议。 对于HTTP(S)代理,后端可以是FastCGI服务器,如PHP-FPM或其他Web服务器。

对于FastCGI后端,就像你需要fastcgi模块 。 例如,您需要使用fastcgi_pass定义后端。 对于代理另一个网站,您需要HTTP代理模块 。 你需要使用像proxy_passproxy_cache这样的方向来控制这个模块的行为。

  1. 我是否需要添加到相同的configuration文件的proxy_pass,proxy_cache等..configuration?

  1. 我是否需要为特定路线设置configuration? 或禁用它们?

例如,您需要代理特定的urlwww.example.com/myawesomeapp ,然后使用位置来匹配url

 location /myawesomeapp { proxy_pass http://<upstream_block_name>; ... other parameter ...; } 
  1. 如果例如,如果我不想要的path/app_dev.php/abc被caching? 我需要做什么?

使用proxy_cache_bypass 。 你可以按照这个教程来设置if指令。