該配置以example.com作為域名,請根據自己的實際情況進行替換。
提醒 此配置僅允許web目錄下app.php和app_dev.php兩個入口文件以PHP腳本方式運行,web目錄下存在的其他PHP文件(比如另外定義了其他的Symfony2入口文件,或者其他PHP程序也安裝在該目錄下)如果被訪問,將被用戶下載。
server { listen 80; server_name example.com; # 域名 root /var/www/symfony2/web; # 站點根目錄 error_log /var/log/nginx/symfony2.error.log; access_log /var/log/nginx/symfony2.access.log; # 如果URL中包含app.php,則轉發為偽靜態格式 rewrite ^/app\.php/?(.*)$ /$1 permanent; location / { index app.php; try_files $uri @rewriteapp; } location @rewriteapp { rewrite ^(.*)$ /app.php/$1 last; } # 此段為將PHP請求轉交給FastCGI服務,PHP-FPM是非常流行的選項。 location ~ ^/(app|app_dev)\.php(/|$) { fastcgi_pass 127.0.0.1:9000; fastcgi_split_path_info ^(.+\.php)(/.*)$; include fastcgi_params; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; fastcgi_param HTTPS off; } }
以下為HTTPS配置:
server { listen 443; server_name example.com; root /var/www/symfony2/web; ssl on; ssl_certificate /etc/ssl/certs/symfony2.crt; ssl_certificate_key /etc/ssl/private/symfony2.key; error_log /var/log/nginx/symfony2.error.log; access_log /var/log/nginx/symfony2.access.log; rewrite ^/app\.php/?(.*)$ /$1 permanent; location / { index app.php; try_files $uri @rewriteapp; } location @rewriteapp { rewrite ^(.*)$ /app.php/$1 last; } location ~ ^/(app|app_dev)\.php(/|$) { fastcgi_pass 127.0.0.1:9000; fastcgi_split_path_info ^(.+\.php)(/.*)$; include fastcgi_params; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; fastcgi_param HTTPS on; } }