nginx location block only in conf file - php

I was able to put a location block inside the default.conf file and have it work as expected. It passed the correct files from the correct directory over to the php-fpm server and display the expected results.
Now, I want to move that location block to its own .conf file for maintenance and ease of leaving the main/default .conf file alone.
However, this is not working as I had hoped or expected. When I do this, nginx restarts just fine, but when I try to browse, I get a 404 error.
This is the conf file for just the location:
server {
location /myapp/ {
root /opt/myapp/htdocs;
index index.php;
location ~ \.php$ {
#root html;
fastcgi_pass 127.0.0.1:9071;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
}
As I stated, if I copy this exact code into the main default.conf file, then it works. But in its own conf file, it doesnt.
In the end, I want to be able to have a different conf file for each location, where each location represents a different version of my app, running on different version of php-fpm (which are already configured on their own ports).
In Apache I was able to do this using Alias and blocks, each in its own conf file. I am trying to do something similar with nginx.

Related

Nginx conf to invoke xslt script to generate php AND process with php-fpm in one request?

Consider the following non-working nginx.conf fragment:
server {
...
root /var/www/html/example.com/www;
location /dev/ {
root /var/www/html/example.com;
location ~* \.(pdf|js|css|png|jpg|jpeg|gif|ico)$ {
}
fastcgi_split_path_info ^/dev/(.+)()$;
fastcgi_intercept_errors on;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME /var/www/html/example.com/xsltproc.php;
fastcgi_param SCRIPT_NAME $fastcgi_script_name;
fastcgi_pass php-fpm;
location ~ \.php$ {
fastcgi_split_path_info ^(.+\.php)(x)$;
fastcgi_intercept_errors on;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_pass php-fpm;
}
}
location / {
index index.html;
location ~ \.php$ {
...
The objective is to use XSLT to transform .xml into .php and instantaneously serve the generated .php file from the protected /dev/ location all in one step for a fast and easy edit / view cycle.
The xsltproc.php script looks up the resource by $fastcgi_script_name and checks to see if the corresponding .xml or .xsl files are newer than the .php file and, if yes, regenerates it using XSLTProcessor::transformToXML().
When development is complete, the .php files are copied to the www location where they are served in the typical non-xslt manner.
However, the above does not work because, even though location /dev/ matches, location ~ \.php$ is ultimately favored and used such that the .php file is not regenerated. If I temporarily remove the location ~ \.php$ section, the .php file is generated correctly. When the section is restored, the .php file is processed correctly.
How do I configure nginx to invoke xsltproc.php AND process the .php file with php-fpm in the same request?
Meaning I effectively want to invoke php-fpm twice in one request: once for xstlproc.php and once on the generated .php file.
Can an internal redirect be used even though the name of the resource is exactly the same (e.g. /dev/test.php)?
UPDATE:
I have not found a way to invoke php-fpm twice. Adding a rewrite immediately after the xsltproc.php fastcgi_pass call results in a jump for the rewrite without executing the script. No surprise there.
However, I did settle on a solution which was to add:
require($phpfile)
to the end of xsltproc.php to simply include the php file regardless of weather or not it was re-generated and then remove location ~ \.php$. The more I think about it, this is a satisfactory solution. It's only for dev anyway.

Can't load any PHP file or see any error in the logs using nginx and php7

I've just recently installed Nginx 1.8 and PHP7 on my server trying to make it serve my application.
When I make my webroot folder index file an HTML file which just returns Hey, then I can see the page loaded with that text.
However, when I try to replace that index.html file with index.php file I just see a blank page and no errors being spat.
I tried to look into /var/log/nginx/access.log and /var/log/nginx/error.log as-well as my site error log: /var/log/nginx/my-website-error.log. Couldn't find any error there.
I tried to maybe look at the PHP7.0-FPM error log found at /var/log/php7.0-fpm.log but nothing there as-well.
I made sure to edit my /etc/php/7.0/fpm/php.ini file and set display_erorrs = On and error_reporting=E_ALL.
I still can't see any error and I can't execute any PHP code.
My Nginx config file which passes the php execution to FPM looks like:
location ~ \.php$ {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;
fastcgi_index index.php;
include fastcgi_params;
}
Any idea what's going on?
Try to add fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; before including the params file. This parameter defines the script that php should execute.

Nginx Symfony2 Staging Config

i'm trying to configure nginx to serve several releases of one symfony project.
Releasefolder structure:
/var/www/my-project/releases/24/
/var/www/my-project/releases/46/
/var/www/my-project/releases/47/
I'd like to call a URL like "http://my-server/my-project/release/47" which should access /var/www/my-project/releases/47/web/app.php on the server.
I tried for a while now but couldn't find the final solution, any help would be great!
My last try:
server {
server_name my-server;
location / {
# try to serve file directly, fallback to app.php
try_files $uri $uri$2app.php$is_args$args;
}
location ~ /my-project/release/([-a-zA-Z0-9]+)/app\.php(/|$) {
root /var/www/my-project/releases/$1/web;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_split_path_info ^(.+\.php)(/.*)$;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root/app.php;
fastcgi_param HTTPS off;
error_log /var/log/nginx/my-project_$1_error.log;
access_log /var/log/nginx/my-project_$1_access.log;
# 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;
}
}
This config produces following error:
[error] 2272#0: *169 rewrite or internal redirection cycle while internally redirecting to "/my-project/release/47app.phpapp.phpapp.phpapp.phpapp.phpapp.phpapp.phpapp.phpapp.phpapp.phpapp.php"
You are missing a root directive in your server container -- is /var/www the default on your architecture? Otherwise, add a root directive:
root /var/www;
try_files is failing for find a match and will rewrite the URL by appending app.php to it. The resulting URL does not match your other location block, so it gets rewritten again and again.
The final term on your try_files directive is wrong. You have $2 where /web/ should be (probably inherited from a previous experiment).
Using try_files to make a general rewrite is a bad idea, as any mistyped URL could lead to a rewrite loop. Try:
location / {
rewrite ^(/my-project/release/[-a-zA-Z0-9]+)$ $1/app.php;
}
I changed the config to this:
server {
server_name my-server;
root /var/www;
location / {
rewrite ^(/my-project/release/[-a-zA-Z0-9]+)$ $1/app.php;
}
location ~ /my-project/release/([-a-zA-Z0-9]+)/app\.php(/|$) {
alias /var/www/my-project/releases/$1/web;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_split_path_info ^(.+\.php)(/.*)$;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root/app.php;
fastcgi_param HTTPS off;
error_log /var/www/my-project/releases/$1/app/logs/nginx_error.log;
access_log /var/www/my-project/releases/$1/app/logs/nginx_access.log;
# 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;
}
}
Now a call like "http://my-server/my-project/release/47" points correctly to "/var/www/my-project/releases/47/web/app.php".
The problem is now that the url "my-project/release/47" arrives at symfony router which can't find a route to this url.
What is now the right way to keep the url showing up like "//my-server/my-project/release/47" at browser but arrives at symfony as "//my-server/"?
Thank you all for your help, unfortunately i couln't get it working. I think i have to learn more about nginx configuration generally to be able to handle such problems, before i can use it in production.
My workaround for my problem is to create an own nginx conf per release. The file will be created at build process, is then transfered to the webserver, copied to /etc/nginx/conf.d/[releasename].conf and activated by sudo service nginx reload.
A deinstallation script within the release dir removes sources and config, when release no longer needed.

Setting up site with homebrewed nginx got 404 on site? Reason why it is happening?

I found many links regarding this type of topic , so far i still could not solve my problem.
I have just installed nginx via homebrew. Here are the steps that i did :
Added site name to etc/hosts
127.0.0.1 mysite.com
On my usr/local/etc/nginx, i created folder using
mkdir sites
(most instructions i have read so far already have sites-enabled or sites-default on thier setup, but mine was clean so i created one.) Then within the folder i created file just using vim :
vim mysite
then in the file i have this :
server {
listen 80;
server_name mysite.com;
root /Users/myname/mysite/mainsite;
client_max_body_size 10M;
# serve static files
location ~ ^/(images|javascript|js|css|flash|media|static)/ {
expires 30d;
}
location / {
index index.php index.html index.htm;
try_files $uri $uri/ /index.php$is_args$args;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /opt/local/share/nginx/html;
}
location ~ \\.php$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param FF_BOOTSTRAP_ENVIRONMENT dev;
fastcgi_param FF_BOOTSTRAP_CONFIG webroot/dev;
fastcgi_buffer_size 1024k;
fastcgi_buffers 1024 1024k;
fastcgi_busy_buffers_size 1024k;
include /usr/local/etc/nginx/fastcgi.conf;
}
}
After this i include my created folder to nginx.conf and nginx.cnf.default but after this i still get a 404 error. The above configuration on mysite file, except for some directory changes, worked on my other computer but some how i cant replicate for it to work, I tried revising and editing my directory in root but i still get 404. Did I miss some important stuff when configuring? Or what are the other possible reasons why i cannot access mysite.com after the above configuration or how i would get 404. Also i think no other background applications are currently running because i have just restarted the computer to see it the site doesnt work.. Any more suggestions why this might be happening? Thanks in advance
404 :(
First of all you mentioned the missing sites-enabled part, probably cause you're using centos or some other distro, I've explained this part on my answer on another question
Your site isn't working because nginx can't see the config file, simply creating a folder in anywhere doesn't work, you need to tell nginx to look into you config file, if you're configuration is in usr/local/etc/nginx like you said, then you need to move this config file mysite to usr/local/etc/nginx/conf.d at least,
or create the sites-available, sites-enabled pair like I explained in my other answer and move mysite to sites-available then symlink to it inside sites-enabled
Of course make sure you point things to the right path since your nginx lives inside usr/local/etc/nginx instead of /etc/nginx

nginx + php with drupal + codeigniter in separate folders

I have a Slicehost slice for a dev server, with nginx and PHP.
I'm trying to get drupal running on localhost/drupal and a codeigniter app running on localhost/codeigniter.
I can get one or the other to work, but not both -- the rewrite and fastcgi seem to be interfering with one another.
Does anyone know how to have /drupal and /codeigniter both working, with rewrite rules (for SEF URLs), in separate folders in my /var/www?
Cheers.
Ok, you have to create a file (no extension needed) in /etc/nginx/sites-available that represents the name of your folder/domain (ex: drupal, yoursite.com).
Here's a sample file:
server {
server_name yourdomain.com;
root /var/www/yourdomain;
index index.php;
location / {
autoindex on;
try_files $uri $uri/ /index.php;
}
location ~ \.php$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
include fastcgi_params;
}
}
In the sample above it will actually send url rewrites to $_SERVER['REQUEST_URI']. For more nginx rewrites, you can take a look at http://wiki.nginx.org/HttpRewriteModule for more reference.
Then you want to enable it by creating a symlink of this file in your /etc/nginx/sites-enabled folder
Example: # ln -s /etc/nginx/sites-available/yoursite /etc/nginx/sites-enabled/yoursite
Then restart/reload nginx
# services nginx reload

Categories