I want that app.php is executable, just like index.php. How can I set this up?
It's for symfony, because symfony has no index.php.
it'is already executable! I think you mean to make apache to look for app.php instead of index.php, to do that you should config your .htaccess file and add somethink like that :
DirectoryIndex app.php
Example for a local virtual host with php5-fpm
server {
listen 80;
server_name your.site.lan;
root /var/www/your.site.lan/web;
error_log /var/log/nginx/site.error.log;
access_log /var/log/nginx/site.access.log;
# strip app.php/ prefix if it is present
rewrite ^/app\.php/?(.*)$ /$1 permanent;
location / {
index app.php;
try_files $uri #rewriteapp;
}
location #rewriteapp {
rewrite ^(.*)$ /app.php/$1 last;
}
# pass the PHP scripts to FastCGI server from upstream phpfcgi
location ~ ^/(app|app_dev|index|app_test|config|info|apc)\.php(/|$) {
fastcgi_pass phpfcgi;
fastcgi_read_timeout 600;
fastcgi_split_path_info ^(.+\.php)(/.*)$;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param HTTPS off;
}
}
where phpfcgi is alias configured at nginx.conf at http section
upstream phpfcgi {
server unix:/var/run/php5-fpm.sock;
}
Related
I have fully worked example of virtual host for a project on Symfony from ()[].
There is a code:
server {
listen 80;
server_name localhost www.localhost;
root /vagrant/web;
error_log /var/log/nginx/error.log;
access_log /var/log/nginx/access.log;
location / {
# try to serve file directly, fallback to app.php
try_files $uri /app_dev.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;
}
}
But I want to improve it a bit for me. How can I load maintenance.html by default instead of app_dev.php?
P.S. I need a behaviour like with try_files $uri /maintenance.html$is_args$args; instead of try_files $uri /app_dev.php$is_args$args;, but ONLY if maintenance.html exists
Solution from here with bit of comments
location / { #can be any location or even in server
if (-f $document_root/maintenance.html) {
return 503; #503 for search engines
}
... # the rest of your config goes here
}
error_page 503 #maintenance;
location #maintenance {
rewrite ^(.*)$ /maintenance.html break;
}
I solved it with:
try_files $uri /maintenance.html$is_args$args /app_dev.php$is_args$args;
I saw that many people had problem configuring one nginx server to have multiple symfony2 applications. However, none wanted the same things and had the same problem as me.
What I want to do is to have multiple applications on the same domain. One main application will answer directly to the domain, and the others will be on alias subdirectory.
With a schema :
http://mydomain/ -> main app
http://mydomain/subdir1 -> another app
http://mydomain/subdir2 -> yet another app
I tried by myself to do that and the main app works perfectly. But the subdirectories are most of the time intercepted by the main app, which throws 404. When I try to add app.php in the URL of a subdirectory (like http://mydomain/subdir1/app.php/my/route), the server return 404.
This is what I did until now :
server {
listen 80;
server_name mydomain;
root /server/www/main-app/web;
location / {
# try to serve file directly, fallback to app.php
try_files $uri /app.php$is_args$args;
# 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;
}
}
location /subdir1/ {
alias /server/www/other-app1/web;
# try to serve file directly, fallback to app.php
try_files $uri /server/www/other-app1/web/app.php$is_args$args;
# PROD
location ~ ^/other-app1/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;
}
}
}
Thanks you for your help !
EDIT 26/12/2014 :
For those who did not understand exactly what I want : I want to host multiple symfony2 apps on the same domain name without subdomain. Without subdomain, I must use subdirectory. Before that I tried nginx, I used Apache2 and it was easy to do the trick with Alias.
I did more search and found out that "alias" and "try_files" aren't good friends (see this bug report : http://trac.nginx.org/nginx/ticket/97). So I activated debug mode and did many tests.
Now I almost did it. The main apps no longer intercepts subdirectories and the others apps answer.
But those others apps answer by 404 so I looked in their logs. And I found out that they looked for URL pattern with the subdirectory in it. For instance they searched /subdir1/login instead of /login.
So this is my new configuration :
server {
listen 80;
server_name mydomain;
root /server/www/main-app/web;
location #rewriteapp {
rewrite ^(.*)$ /app.php/$1 last;
}
location /subdir1/ {
set $root "/server/www/other-app1/web";
# try to serve file directly, fallback to app.php
try_files $uri #rewriteapp;
}
location / {
index app.php;
set $root "/server/www/main-app/web";
# try to serve file directly, fallback to app.php
try_files $uri #rewriteapp;
}
# PROD
location ~ ^/app\.php(/|$) {
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_split_path_info ^(.+\.php)(/.*)$;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $root$fastcgi_script_name;
fastcgi_param SCRIPT_NAME $fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info;
}
}
As you can see, the trick was to not use $document_root for the SCRIPT_FILENAME and I created my own instead. I don't know how the symfony2 router search the pattern in the URL, but with my previous configuration (Apache2) I never had this problem. So maybe their is another trick to send the correct path to script app.php.
Thanks you again for your help !
This solved it finally for me (thanks to Claros answer), after million things i tried. Like this, urls like the following work:
/abc/path/to/endpoint
but not /abc/app.php/path/to/endpoint. Config.php and App_dev.php, if in web folder are given back as plain text.
I still try to figure out to get /abc to work (/abc/ works but /abc not). There i get a Symfony exception that route /abc can not be found.
Also some font urls (for bootstrap) are still incorrect but styles, routing etc works.
location /abc {
set $subpath /abc;
set $sfPath /var/www/abc/current/web;
alias $sfPath;
try_files $uri #rewrite;
}
location / {
set $subpath "";
set $sfPath /var/www/def/current/web;
alias $sfPath;
try_files $uri #rewrite;
}
location #rewrite {
rewrite ^(.*)$ $subpath/app.php$1 last;
}
location ~ /app\.php(/|$) {
internal;
include /etc/nginx/fastcgi_params;
fastcgi_index app.php;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_param DOCUMENT_ROOT $sfPath;
fastcgi_param SCRIPT_FILENAME $sfPath/app.php;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
}
If you want also the app_dev.php to work in demo environment the best way i found is the following (to have the php block everytime inside the location block):
location /xyz {
set $subpath /xyz;
set $sfPath /var/www/xyz/current/web;
alias $sfPath;
try_files $uri #rewrite;
#Change the match for app_dev.php to work
location ~ /(app|app_dev|config)\.php(/|$) {
#Drop the internal for App_dev.php to work
#internal;
include /etc/nginx/fastcgi_params;
fastcgi_index app.php;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_param DOCUMENT_ROOT $sfPath;
fastcgi_param SCRIPT_FILENAME $sfPath/app.php;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
}
}
After many hours of debugging, I finally solved the problem. This is my final configuration :
server {
listen 80;
server_name mydomain;
root /server/www;
location #rewriteMainApp {
rewrite ^(.*)$ /app.php/$1 last;
}
location #rewriteOtherApp1 {
rewrite ^(.*)$ /subdir1/app.php/$1 last;
}
location /subdir1 {
alias /server/www/other-app1/web;
index app.php;
set $subfolder "other-app1/web";
try_files $uri #rewriteOtherApp1;
}
location / {
root /server/www/main-app/web;
index app.php;
set $subfolder "main-app/web";
try_files $uri #rewriteMainApp;
}
# PROD
location ~ /app\.php(/|$) {
fastcgi_pass unix:/var/run/php5-fpm.sock;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root/$subfolder/app.php;
}
}
Thanks you all for your help !
Seperate your applications with-in another server tag in your sites-enabled file.
For example:
#Site 1
server {
#Configuration
}
server {
#Configuration 2
}
server {
#Configuration 3
}
Sample configuration:
server {
listen 80;
root /var/www/yourdomain.com/web;
server_name yourdomain.com www.yourdomain.com;
add_header X-UA-Compatible "IE=Edge,chrome=1";
location ~* \.(css|js|gif|jpe?g|png)$ {
expires 1y;
add_header Pragma public;
add_header Cache-Control "public, must-revalidate, proxy-revalidate";
}
location / {
try_files $uri #rewriteapp;
}
location #rewriteapp {
rewrite ^(.*)$ /app_dev.php/$1 last;
}
location ~ ^/(app|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;
}
error_log /var/log/nginx/yourdomain.com.error.log;
access_log /var/log/nginx/yourdomain.com.access.log;
}
server {
listen 80;
root /var/www/yourdomain.com/anotherproject/web;
server_name sub1.yourdomain.com www.sub1.yourdomain.com;
add_header X-UA-Compatible "IE=Edge,chrome=1";
location ~* \.(css|js|gif|jpe?g|png)$ {
expires 1y;
add_header Pragma public;
add_header Cache-Control "public, must-revalidate, proxy-revalidate";
}
location / {
try_files $uri #rewriteapp;
}
location #rewriteapp {
rewrite ^(.*)$ /app_dev.php/$1 last;
}
location ~ ^/(app|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;
}
error_log /var/log/nginx/sub1.yourdomain.com.error.log;
access_log /var/log/nginx/sub1.yourdomain.com.access.log;
}
I'm trying to get a clear understanding of what's going on with my nginx configuration file for Symfony2, here it is:
server {
listen 80;
autoindex on;
server_name example.com;
root /var/www/example.com/web;
rewrite ^/app\.php/?(.*)$ /$1 permanent;
location / {
index app.php;
try_files $uri $uri/ #symfony;
}
location #symfony {
rewrite ^(.*)$ /app.php/$1 last;
}
location ~ ^/app\.php(/|$) {
include fastcgi_params;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_split_path_info ^(.+\.php)(/.*)$;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param SCRIPT_NAME $fastcgi_script_name;
}
}
In short, I'm trying to strip app.php in every possible URI that a user can set e.g. example.com/app.php/demo or just example.com/app.php.
This config actually works for URIs like ones above, but it leads to a redirect loop in case of trying to access "root" URI example.com.
And if I remove $uri/ from try_files and leave only $uri and a fallback #symfony there, everything is working fine except I can't access any directories as they're going to be processed by SF.
I'm out of ideas, did a lot of research on how nginx and rewrites actually work, but as for now it's a dead end for me. If you can find a solution to stay with $uri/ in try_files and get out of a loop at front, please let me know.
This solution from Nginx Tips worked for me. It's almost, but not quite, the same as yours.
server {
server_name domain.tld www.domain.tld;
root /var/www/project/web;
location / {
# try to serve file directly, fallback to rewrite
try_files $uri #rewriteapp;
}
location #rewriteapp {
# rewrite all to app.php
rewrite ^(.*)$ /app.php/$1 last;
}
location ~ ^/(app|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;
}
error_log /var/log/nginx/project_error.log;
}
I am running Wordpress with nginx and the URL rules rewriting is over my head. I don't understand why is it not working.
Should be:
https://www.impotencetointimacy.com
But redirects to:
https://www.impotencetointimacy.com/impotencetointimacy.com/faq/
I have removed the .htaccess file.
Here is my nginx config:
server {
listen 66.216.100.111:80;
server_name impotencetointimacy.com www.impotencetointimacy.com;
root /home/mydir;
error_log /home/mydir/error_log;
location / {
index index.php index.html index.htm;
# return 301 https://$host$request_uri;
}
location ~ \.php$ {
root /home/mydir;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /home/mydir/$fastcgi_script_name;
include fastcgi_params;
}
if (!-e $request_filename)
{
rewrite ^(.+)$ /index.php?q=$1 last;
}
#if (!-e $request_filename) {
# rewrite ^.* /index.php break;
#}
}
Another config is for SSL vhost.
What is going wrong?
I had a problem which I created a Multisite Wordpress.
I used Nginx for my web server.
Multisite was enabled on Wordpress which a main site is freelayers.net
When I created a new website everything is work well. I can recive a successful email. But when I try to access the subsites like cantho.freelayers.net. It always redirect me to freelayers.net
I had also add a wildcard record (*) on DNS manager. It looks like:
(IP address /URL) my server IP (Record Type) A/Address
I thinks something wrong in my conf file on Nginx but I can find any solution
This is configuration file for the main site.
server {
listen 9880;
server_name www.freelayers.net;
rewrite ^/(.*) /wp-includes/ms-files.php?file=$1;
root /home/web/freelayers;
index index.html index.htm index.php;
#if (!-e $request_filename) {
# rewrite ^/(.*)$ /index.php?q=$1 last;
#}
access_log /home/web/freelayers/logs/access.log;
error_log /home/web/freelayers/logs/error.log;
location ~* \.(js|css|png|jpg|jpeg|gif|ico)$ {
expires max;
log_not_found off;
}
location / {
if (!-e $request_filename){
rewrite ^(.*)$ /index.php break;
}
}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
location ~ \.php$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
location ~ /\.ht {
deny all;
}
}
Please give some ideas about this issue! Thanks for any reply!