I want to configure my symfony 2 application so that all /web/app.php are omited, unless that's /web/app_dev.php, then I want to enter the development environment.
The problem is that I always get 502 bad gateway error. What's wrong?
This is my current configuration:
server {
listen 80;
# Server name being used (exact name, wildcards or regular expression)
server_name domain.com;
client_max_body_size 20M;
# Document root, make sure this points to your Symfony2 /web directory
root /var/www/domain.com/web;
rewrite ^/app\.php/?(.*)$ /$1 permanent;
# Logging
error_log /var/log/nginx/domain.com.error.log;
access_log /var/log/nginx/domain.com.access.log;
location / {
index app.php;
try_files $uri #rewriteapp;
}
location #rewriteapp {
rewrite ^(.*)$ /app.php/$1 last;
}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
location ~ ^/(app|app_dev)\.php(/|$) {
fastcgi_pass 127.0.0.1:9000;
fastcgi_split_path_info ^(.+\.php)(/.*)$;
include fastcgi.conf;
fastcgi_index index.php;
}
}
Related
I'm trying to change from plain http://sitename/?p=123 to http://sitename/postname with rewrite rule but it gives error 404 Not found. This is my nginx server block code:
server {
listen 80;
listen 443 ssl;
root /var/www/sitename/html;
index index.php index.html index.htm;
server_name sitename www.sitename;
client_max_body_size 10M;
# Certificates handled by CertBot
location ~ \.php$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
location / {
server_name sitename www.sitename;
# First attempt to serve request as file, then
# as directory, then fall back to displaying a 404.
# try_files $uri $uri/ /index.php$is_args$args;
# rewrite ^ http://$server_name$request_uri permanent;
rewrite ^/(.*)$ http://$server_name/$1 permanent;
# Uncomment to enable naxsi on this location
# include /etc/nginx/naxsi.rules
# root /data/www;
# index index.html index.htm;
}
}
I want to use rewrite so I redirect https of front-end to http and wp-admin and wp-login as https which is handled by Wordpress.
How can I fix it?
Created a separate similar server block with 443 listen. Included the cert lines and removed the location from 443 block.
I've been attempting to upgrade to php 7.1 using phpbrew, and elected to install it with nginx, as I read everywhere that it was simpler than Apache (not that simple, in my humble opinion).
As I was trying to run Symfony2 with nginx, I came across this doc page, which gives a basic config for Sf2 on nginx.
I managed to configure php-fpm to serve app_dev.php, and every file ending in .php correctly. However, as soon as I go to a different URL (/home for instance), the nginx config breaks and I get a File not found error in php-fpm.
How do I configure the nginx virtual host to allow for everything after app_dev.php or app.php to be rewritten (as it would with modrewrite on apache2)?
My nginx file for reference:
server {
listen 80 default_server;
listen [::]:80 default_server ipv6only=on;
root /usr/share/nginx/html;
index index.html index.htm;
server_name localhost;
location / {
try_files $uri $uri/ =404;
}
location /my-app {
index web/app_dev.php;
try_files $uri /web/app.php$is_args$args;
}
location /dist {
root /usr/share/nginx/html;
index depp/index.php;
try_files $uri /depp/index.php$is_args$args;
}
location ~ \.php$ {
fastcgi_pass unix:/home/gabriel/.phpbrew/php/php-7.1.0/var/run/php-fpm.sock;
fastcgi_split_path_info ^(.+\.php)(/.*)$;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
fastcgi_param DOCUMENT_ROOT $realpath_root;
fastcgi_param REQUEST_URI $uri?$args;
}
}
You are missing a rewrite condition to catch-all the incoming requests and forward them to your front controller.
Try something like:
# strip app.php/ prefix if it is present
rewrite ^/app\.php/?(.*)$ /$1 permanent;
location /my-app {
index app.php;
try_files $uri #rewriteapp;
}
location #rewriteapp {
rewrite ^(.*)$ /app.php/$1 last;
}
# Symfony 2 app index
location ~ ^/app\.php(/|$) {
fastcgi_pass unix:/home/gabriel/.phpbrew/php/php-7.1.0/var/run/php-fpm.sock;
fastcgi_split_path_info ^(.+\.php)(/.*)$;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
# deny access to any other php files
location ~^.*\.php(/|$) {
deny all;
}
You current configuration is a more general configuration for any .php script but Symfony2 and framework in general only provide a catch-all front-controller.
I would like to have WordPress installed in my Laravel 4 project at /public/cms, I don't need WordPress pages to be accessible through the browser but I do need to Post to WordPress routes to update WP content (forms) from within laravel.
I have tried updating my vhosts file to match with the answer here: How to install WordPress alongside Laravel on Nginx with pretty permalinks (SEO-friendly URLs)?
But this is causing a redirect loop.
here is my nginx vhost file:
server {
listen 80;
server_name laravel.mylifemysmile.org;
root /var/www/mylifemysmile/public;
# Point index to the Laravel front controller.
index index.php;
location /cms/ {
try_files $uri $uri/ #wordpress;
}
location #wordpress {
rewrite /cms/ /cms/index.php;
}
location ^/cms/index.php(/.*)?$ {
fastcgi_split_path_info ^(/cms/index.php)(/.*)$;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info;
include fastcgi_params;
}
location / {
try_files $uri $uri/ /index.php$query_string;
}
# Remove trailing slash to please routing system.
if (!-d $request_filename) {
rewrite ^/(.+)/$ /$1 permanent;
}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
location ~ \.php$ {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
# NOTE: You should have "cgi.fix_pathinfo = 0;" in php.ini
# With php5-fpm:
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
}
Update
Here's the new nginx vhost (server block) file:
server {
listen 80;
server_name laravel.mylifemysmile.org;
root /var/www/mylifemysmile/public;
# Point index to the Laravel front controller.
index index.php;
location / {
try_files $uri $uri/ /index.php$query_string #laravel;
}
# Remove trailing slash to please routing system.
if (!-d $request_filename) {
rewrite ^/(.+)/$ /$1 permanent;
}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
location ~ \.php$ {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
set $php_root /var/www/mylifemysmile/public;
if ($request_uri ~ /cms/) {
# ok, this line is a bit confusing, be aware
# that path to /cms/ is already in the request
# so adding a trailing /cms here will
# give a "no input file" message
set $php_root /var/www;
}
# NOTE: You should have "cgi.fix_pathinfo = 0;" in php.ini
# With php5-fpm:
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $php_root$fastcgi_script_name;
}
# now wordpress, remember this lives in a sibling directory of the main app
location ~ /cms/ {
try_files $uri $uri/ /index.php$args;
# again, this might look a bit weird,
# but remember that root directive doesn't drop
# the request prefix, so /cms is appended at the end
root /var/www;
if (!-d $request_filename)
{
rewrite ^/(.*)/$ /$1 permanent;
}
}
}
I moved wordpress out of laravel/public and into its own folder on the same level as laravel. I can now access WordPress content (including images) in the laravel site, but still can't access the wordpress dashboard.
Im using nginx with a very simple configuration, it works for all php sites in the subdirs of /usr/share/nginx/www/.
But now id like to make a new project in a subdirectory with rewrite rules.
So i decide to make a .conf for this beside the default.
But the rewriting is not working cause of the error "rewrite or internal redirection cycle".
default
server {
listen 80 default_server;
# listen [::]:80 default_server ipv6only=on;
root /usr/share/nginx/www;
index index.php index.html index.htm;
# Make site accessible from http://localhost/
server_name localhost;
location / {
# First attempt to serve request as file, then
# as directory, then fall back to displaying a 404.
try_files $uri $uri/ /index.html;
# Uncomment to enable naxsi on this location
# include /etc/nginx/naxsi.rules
}
location /doc/ {
alias /usr/share/doc/;
autoindex on;
allow 127.0.0.1;
allow ::1;
deny all;
}
location ~ \.php$ {
try_files $uri =404;
# fastcgi_split_path_info ^(.+\.php)(/.+)$;
# NOTE: You should have "cgi.fix_pathinfo = 0;" in php.ini
# With php5-cgi alone:
# fastcgi_pass 127.0.0.1:9000;
# With php5-fpm:
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
fastcgi_intercept_errors on;
}
location ~ /\. {
access_log off;
log_not_found off;
deny all;
}
}
newproject.conf
# nginx configuration
autoindex off;
location /newproject/ {
if (!-e $request_filename) {
rewrite ^/newproject/(.+)$ /newproject/index.php?url=$1 break;
}
}
# First attempt to serve request as file, then
# as directory, then fall back to displaying a 404.
try_files $uri $uri/ /index.html;
This part about fall back to 404 is wrong. Probably you have missed =404 here, which resulted in redirection cycle (it redirects to /index.html again and again).
Please note from the documentation:
If none of the files were found, an internal redirect to the uri specified in the last parameter is made.
Currently I have this piece of code:
if (!-e $request_filename)
{
rewrite ^/(.*)$ /index.php?/$1 last;
break;
}
This works OK for the following:
example.com/foo redirects to index.php
However example.com/foo?bar doesn't work. How do you make it work?
FWIW: I don't experience this problem in Apache's mod_rewrite equivalent. Basically, I moved a site that works from Apache to Nginx. Now I experience this issue.
Edit:
To be clear here's what I indent to do:
example.com/foo
example.com/foo/bar/etc
example.com/foo?bar
example.com/foo?bar=quz
Should all serve index.php "silently" without changing the URL of the browser's address bar.
I just tested it with the following config, and I believe this does want you want:
server {
#listen 80; ## listen for ipv4; this line is default and implied
#listen [::]:80 default ipv6only=on; ## listen for ipv6
root /home/www/test;
index index.php index.html index.htm;
# Make site accessible from
server_name test.myhost.nl;
location / {
# First attempt to serve request as file, then as directory, then fall back to index.php
try_files $uri $uri/ /index.php?$args;
}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
location ~ \.php$ {
try_files $uri = 404;
# Fix for server variables that behave differently under nginx/php-fpm than typically expected
fastcgi_split_path_info ^(.+\.php)(/.+)$;
# Include the standard fastcgi_params file included with ngingx
include fastcgi_params;
fastcgi_param PATH_INFO $fastcgi_path_info;
fastcgi_index index.php;
# Override the SCRIPT_FILENAME variable set by fastcgi_params
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
# Pass to upstream PHP-FPM; This must match whater you name your upstream connection
#fastcgi_pass phpfpm;
fastcgi_pass 127.0.0.1:9000;
}
}