I try to make my production server to serve my applications in a same style as phpmyadmin is served. Like example.com/phpmyadmin and what I try to do is example.com/myappname.
My nginx/sites-available/default looks like this:
server {
listen 80 default_server;
listen [::]:80 default_server ipv6only=on;
root /var/www;
index index.html index.htm index.php;
server_name _;
location ~ \.php$ {
#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;
}
location /myapp {
root /var/www/myapp/public/;
index index.php;
}
location /phpmyadmin {
root /usr/share/;
index index.php index.html index.htm;
location ~ ^/phpmyadmin/(.+\.php)$ {
try_files $uri =404;
root /usr/share/;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include /etc/nginx/fastcgi_params;
}
location ~* ^/phpmyadmin/(.+\.(jpg|jpeg|gif|css|png|js|ico|html|xml|txt))$ {
root /usr/share/;
}
}
location /phpMyAdmin {
rewrite ^/* /phpmyadmin last;
}
location ~ /\.ht {
deny all;
}
}
So if someone has a working conf for this, I would like to have it too!
Notice that your /phpmyadmin location has extra configuration to pass the request to PHP-FPM via the fast_cgi configurations.
You'll need to do that for your /myapp location as well.
You may want to change your myapp location to something like this:
location ^/myapp/(.+\.php)$ {
root /var/www/myapp/public/;
index index.php;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
include /etc/nginx/fastcgi_params;
}
For comparison, here is my Laravel nginx configuration - however I don't have my app running under a "sub-directory".
Try changing some configuration items to see what you can get to work. Note that I removed fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; from the Laravel app location - I dont believe it is necessary.
Lastly, would this make more sense on ServerFault? You may get better answers there.
Related
I have two apps. One is simple php file and the other is a zend framework app. I am using Nginx. It is working for simple php app which is accessible via example.com/.
For the ZF3 app, nginx is working only for parent route and none of the child routes are working. The parent route is accessible via example.com/products. I want nginx to handle route actions such as example.com/products/add, example.com/products/view which are already defined in module config.
nginx config
server {
listen *:80;
server_name example.com;
index index.html index.htm index.php;
access_log /var/log/nginx/simpleapp.access.log combined;
error_log /var/log/nginx/simpleapp.error.log;
root /var/www/simpleapp;
location ~ \.php$ {
fastcgi_pass unix:/var/run/php/php7.1-fpm.sock;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
location ^~ /products {
alias /var/www/ecom/public;
try_files $uri $uri/ /index.php$is_args$args;
location ~ \.php$ {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php/php7.1-fpm.sock;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $request_filename;
}
}
}
nginx log file
FastCGI sent in stderr: "Primary script unknown" while reading response header from upstream
What works:
example.com/
example.com/products
What doesn't work:
example.com/products/add
example.com/products/view
example.com/products/categories
Do you really need two different declarations for very same php processing? I would stick with one and only differentiate the root using alias on /product for example:
server {
listen *:80;
server_name example.com;
index index.html index.htm index.php;
access_log /var/log/nginx/simpleapp.access.log combined;
error_log /var/log/nginx/simpleapp.error.log;
root /var/www/simpleapp;
location / {
try_files $uri $uri/ /index.php$is_args$args;
}
location /products {
alias /var/www/ecom/public;
}
location ~ \.php$ {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_index index.php;
fastcgi_pass unix:/var/run/php/php7.1-fpm.sock;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
}
I have a problem with the configuration of nginx. Structure directory in my server is:
/var/www/public_html is my page.
/var/www/pma - phpmyadmin
/var/www/vimbadmin - ViMbAdmin
My /etc/nginx/sites-available/default
server{
listen 80 default_server;
listen [::]:80 default_server ipv6only=on;
access_log /var/www/log/access.log;
error_log /var/www/log/error.log;
root /var/www/public_html;
index index.php index.htm;
# Make site accessible from http://localhost/
server_name localhost;
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
location ~ \.php$ {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
include fastcgi_params;
}
location /pma/ {
index index.php;
alias /var/www/phpmyadmin/;
location ~ \.php$ {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_intercept_errors on;
fastcgi_ignore_client_abort off;
}
}
location /vma/ {
index index.php;
alias /var/www/vimbadmin/public/;
try_files $uri $uri/ /index.php?$args;
index index.php;
location ~\.php$ {
fastcgi_pass unix:/var/run/php5-fpm.sock;
#fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
#try_files $uri =404;
include fastcgi_params;
}
}
}
Now when I go: domian.com/vma/ redirect me to domian.com/vma/auth/login and I see:
File not found.
Phpmyadmin works.
I know that the configuration is bad... But where is mistake?
I did something like this
location ~ ^/vma {
alias /usr/local/vimbadmin/public;
location ~ ^/vma/(.*\.(js|css|gif|jpg|png|ico))$ {
alias /usr/local/vimbadmin/public/$1;
}
rewrite ^/vma(.*)$ /vma/index.php last;
location ~ ^/vma(.+\.php)$ {
alias /usr/local/vimbadmin/public$1;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
charset utf8;
include fastcgi_params;
fastcgi_param DOCUMENT_ROOT /usr/local/vimbadmin/public;
}
}
it works, at least on my machine ;)
The answer of ir1keren did not work for me with ViMbAdmin V3.0.11.
Here is how I did it:
#
# ViMbAdmin 3.0.x Nginx configuration
# Directory where ViMbAdmin is installed
set $vimbadmin /var/www/vimbadmin;
location /vma {
alias $vimbadmin/public;
index index.php index.html index.htm;
location ~ ^/vma/(.*\.(js|css|gif|jpg|png|ico))$ {
alias $vimbadmin/public/$1;
}
rewrite ^/vma(.*)$ /mail/index.php last;
# Pass PHP scripts to FastCGI server
location ~ \.php$ {
#try_files $uri =404;
# NOTE: You should have "cgi.fix_pathinfo = 0;" in php.ini
fastcgi_split_path_info ^/vma/(.+\.php)(/.+)$;
include fastcgi_params;
fastcgi_param DOCUMENT_ROOT $realpath_root;
fastcgi_param SCRIPT_FILENAME $request_filename;
fastcgi_param APPLICATION_ENV production;
fastcgi_index index.php;
fastcgi_pass php-backend;
fastcgi_intercept_errors on;
}
}
My folder structure is as follows:
/www
/api.domain.tld
/app.domain.tld
The API contains the system it self and APP implements the API via HTTP.
I want to create an Nginx server for app.domain.tld that also contains an "virtual directory" for API.
You can contact the API likes this: http://api.domain.tld/method/api.json
But it would be great if the API can be contacted like this also: http://app.domain.tld/api/method/api.json without copying something into APP, but keep those two "systems" separated.
What I have for now:
server {
listen 80;
root /var/www/app.domain.tld;
index index.php index.html;
server_name app.domain.tld;
location ^~ /api {
alias /var/www/api.domain.tld;
location ~ \.php$ {
try_files $uri = 404;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
rewrite ^/api/([a-z]+)/api.json$ /api.php?method=$1 last;
}
location....
location....
location....
location ~ \.php$ {
try_files $uri = 404;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
Unfortunately this does now works as expected.
The rewrite does not work at all. I can get api.domain.tld/index.php but when it needs to use the rewrite, it will not work.
I have tried several things. Either I get 404 or 403 with this error:
directory index of [path] is forbidden
Can someone come up with a better solution that actually works?
Regards
You should change SCRIPT_FILENAME path:
server {
listen 80;
root /var/www/app.domain.tld;
index index.php index.html;
location ~ ^/api/(.+\.php)$ {
alias /www/api.domain.tld/public/$1;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $request_filename;
include fastcgi_params;
}
}
I have the following server settings, and for some reason, my document root is going to the wrong location. Why is it doing that? $1 is correct on the return line, but why is it pulling the wrong root?
This block is broken:
server{
listen 80;
#server_name mission13.io www.mission13.io;
server_name "~^www\.(.*)$";
return 301 $scheme://$1$request_uri;
root /usr/share/nginx/html/$1;
index index.php index.html index.htm;
location / {
try_files $uri $uri/ /index.html;
}
include /usr/share/nginx/conf/mission13.io.conf;
location ~ \.php$ {
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
This block works:
server{
listen 80;
root /usr/share/nginx/html/diskise.com;
index index.php index.html index.htm;
server_name diskise.com www.diskise.com;
location / {
try_files $uri $uri/ /index.html;
}
include /usr/share/nginx/conf/diskise.com.conf;
location ~ \.php$ {
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
it is using a root that is set somewhere else, a completely different file.
I am relatively new to NGINX, I admit (less than 24 hours), but, I got it basically configured. I'm setting up a site for a friend, and I saw an example of some code that uses subdomains like in the code below. But, in the subdomain, PHP won't work. It just asks me to download the file if I go to "subdomain.domain.tld", but, if I go to "subdomain.domain.tld/index.php", it says "No input file specified." The subdomain is phpmyadmin, by the way.
server {
listen 80;
server_name irc.physibots.info;
rewrite (.*) http://physibots.info:3989;
}
server {
listen 80;
server_name "~^([a-z]+)?.physibots.info";
root /home/virtual/physibots.info/subdomains/$1;
index index.php index.html index.html;
location / {
autoindex on;
}
location ~ \.php {
try_files $uri /error.html
fastcgi_index index.php;
fastcgi_pass unix:/tmp/php.socket;
include fastcgi_params;
fastcgi_split_path_info ^((?U).+\.php)(/?.+)$;
fastcgi_param PATH_INFO $fastcgi_path_info;
fastcgi_param PATH_TRANSLATED $document_root$fastcgi_path_info;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
}
#server {
# listen 443;
# server_name localhost;
#
# charset utf-8;
#
# ssl on;
# ssl_certificate
server {
listen 80;
server_name physibots.info default;
root /home/virtual/physibots.info/public_html;
index index.php index.html index.html;
location / {
autoindex on;
}
location ~ \.php {
try_files $uri /error.html
fastcgi_index index.php;
fastcgi_pass unix:/tmp/php.socket;
include fastcgi_params;
fastcgi_split_path_info ^((?U).+\.php)(/?.+)$;
fastcgi_param PATH_INFO $fastcgi_path_info;
fastcgi_param PATH_TRANSLATED $document_root$fastcgi_path_info;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
}
Move the try_files to your location / { } block and change it to try_files $uri $uri/ /index.php;
location / {
autoindex on;
try_files $uri $uri/ /index.php;
}
location ~ \.php {
fastcgi_index index.php;
fastcgi_pass unix:/tmp/php.socket;
The rest looks surprisingly good for a beginner. :)
Also- make sure you're testing with curl and not a web browser or you'll constantly fight caching.