Proper coding of location for php and proxy_pass

forumacct nginx-forum at
Tue Jan 24 08:05:19 UTC 2017

Hello All,

What is the correct coding of location section(s) for 2 Raspberry with

RPI1 is the main webpage and also is going to host owncloud (which uses
https and php)
RPI2 is a weather station using plain http html and some php.

To catch requests to RPI2 I can use a text pattern in the URL. 'rpi'
Now my problem is that the RPI2 has some php and the RPI1 with owncloud has

I have tried 2 location segments but it seems the php always wins.

What is the correct way to code this?

Here's a tuned down version of the default config that shows the problem.
Just now I have commented out the php. This way 'owncloud is disabled but
all URLs with 'rpi' (including those with php) are send to RPI2.

server {
	listen   8000; ## listen for ipv4; this line is default and implied
	#listen   [::]:80 default_server ipv6only=on; ## listen for ipv6

	root /media/usbstick/nginx/www;
	index index.php index.html index.htm;

	# Make site accessible from http://localhost/
	server_name rpi3;

	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
                ssi on;

        # Pass /rpi Weather station to RPI1
        location /rpi {

	# pass the PHP scripts to FastCGI server listening on
        #location ~ \.php$ {
            #fastcgi_split_path_info ^(.+\.php)(/.+)$;
            ##fastcgi_pass unix:/var/run/php5-fpm.sock;
            #fastcgi_index index.php;
            #include fastcgi_params;

Here is a version of the full config which does not work for the weather

upstream php-handler {
#server unix:/var/run/php5-fpm.sock;

server {
listen 8000;
#server_name localhost;
return 301 https://$server_name$request_uri;  # enforce https

server {
listen 443 ssl;
#server_name localhost;
add_header Strict-Transport-Security "max-age=31536000; includeSubDomains";

ssl_certificate     /etc/letsencrypt/live/;
ssl_certificate_key /etc/letsencrypt/live/;

# Path to the root of your installation
root /media/usbstick/nginx/www/owncloud;

client_max_body_size 2000M; # set max upload size
fastcgi_buffers 64 4K;

rewrite ^/caldav(.*)$ /remote.php/caldav$1 redirect;
rewrite ^/carddav(.*)$ /remote.php/carddav$1 redirect;
rewrite ^/webdav(.*)$ /remote.php/webdav$1 redirect;

index index.php;
error_page 403 /core/templates/403.php;
error_page 404 /core/templates/404.php;

location = /robots.txt {
allow all;
log_not_found off;
access_log off;

location ~ ^/(?:\.htaccess|data|config|db_structure\.xml|README) {
deny all;
location / {
                # The following 2 rules are only needed with webfinger
                rewrite ^/.well-known/host-meta
/public.php?service=host-meta last;
                rewrite ^/.well-known/host-meta.json
/public.php?service=host-meta-json last;

                rewrite ^/.well-known/carddav /remote.php/carddav/
                rewrite ^/.well-known/caldav /remote.php/caldav/ redirect;

                rewrite ^(/core/doc/[^\/]+/)$ $1/index.html;

                try_files $uri $uri/ index.php;

location ~ \.php(?:$|/) {
                fastcgi_split_path_info ^(.+\.php)(/.+)$;
                include fastcgi_params;
                fastcgi_param SCRIPT_FILENAME
                fastcgi_param PATH_INFO $fastcgi_path_info;
                fastcgi_param HTTPS on;
                fastcgi_pass php-handler;

# Optional: set long EXPIRES header on static assets
location ~* \.(?:jpg|jpeg|gif|bmp|ico|png|css|js|swf)$ {
                expires 30d;
                # Optional: Don't log access to assets
                access_log off;

        # Pass /rpi Weather station to RPI1
        location /rpi {


Thanks for helping,

Posted at Nginx Forum:,272114,272114#msg-272114

More information about the nginx mailing list