setting up with nginx rewrites

Get help with installation and running phpBB 3.3.x here. Please do not post bug reports, feature requests, or extension related questions here.
Post Reply
youradds
Registered User
Posts: 19
Joined: Wed Jan 02, 2008 12:54 pm

setting up with nginx rewrites

Post by youradds »

Hi,

I'm trying to get phpBB working on my nginx server. I already had an nginx config file for the domain (and it was running PHP fine). I've been trying to put together

Code: Select all

server {
    listen      123.123.123.123:443 ssl http2;
    listen      [::]:443 ssl http2;
    server_name mysite.net www.mysite.net;
    root        /home/united/web/mysite.net/public_html;
    index       index.php index.html index.htm;
    access_log  /var/log/nginx/domains/mysite.net.log combined;
    access_log  /var/log/nginx/domains/mysite.net.bytes bytes;
    error_log   /var/log/nginx/domains/mysite.net.error.log error;

    ##ssl         on;
    ssl_certificate      /home/united/conf/web/ssl.mysite.net.pem;
    ssl_certificate_key  /home/united/conf/web/ssl.mysite.net.key;

    location = /favicon.ico {
        log_not_found off;
        access_log off;
    }

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

    location / {

        include     /home/united/conf/web/nginx.mysite.net.rules.conf*;

        ssi on;

        location ~* ^.+\.(jpeg|jpg|png|gif|bmp|ico|svg|css|js|eot|otf|ttf|woff|woff2)$ {

            rewrite "/2018/v\d+/js/(.+)\.js$" /2018/js/$1.js last;
            rewrite "/2018/v\d+/css/(.+)\.css$" /2018/css/$1.css last;
            rewrite "/2018/v\d+/js/(.+)\.css$" /2018/js/$1.css last;

            expires     max;
        }

        location /forum/ {
            # phpBB uses index.htm
            index index.php index.html index.htm;
            try_files $uri $uri/ @rewriteapp;
        }

        # Deny access to internal phpbb files.
        location ~ /(config\.php|common\.php|cache|files|images/avatars/upload|includes|(?<!ext/)phpbb(?!\w+)|store|vendor) {
            deny all;
            # deny was ignored before 0.8.40 for connections over IPv6.
            # Use internal directive to prohibit access on older versions.
            internal;
        }


        location ~ \.cgi$ {
            try_files $uri =404;
            gzip off;
            proxy_set_header X-Real-IP  $remote_addr;
            proxy_set_header X-Forwarded-For $remote_addr;
            proxy_set_header Host $host;
            proxy_pass http://127.0.0.1:8181;

        }

        location ~ [^/]\.php(/|$) {
            fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
            if (!-f $document_root$fastcgi_script_name) {
                return  404;
            }

            fastcgi_pass    127.0.0.1:9003;
            fastcgi_index   index.php;
            include         /etc/nginx/fastcgi_params;

            # Necessary for php.
            fastcgi_split_path_info ^(.+\.php)(/.*)$;
            fastcgi_param PATH_INFO $fastcgi_path_info;
            fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
            fastcgi_param DOCUMENT_ROOT $realpath_root;
            try_files $uri $uri/ /app.php$is_args$args;


        }

        # Correctly pass scripts for installer
        location /forum/install/ {
            # phpBB uses index.htm
            try_files $uri $uri/ @rewrite_installapp =404;

            # Pass the php scripts to fastcgi server specified in upstream declaration.
            location ~ \.php(/|$) {
                # Unmodified fastcgi_params from nginx distribution.
                include fastcgi_params;
                # Necessary for php.
                fastcgi_split_path_info ^(.+\.php)(/.*)$;
                fastcgi_param PATH_INFO $fastcgi_path_info;
                fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
                fastcgi_param DOCUMENT_ROOT $realpath_root;
                try_files $uri $uri/ /install/app.php$is_args$args =404;
                fastcgi_pass    127.0.0.1:9003;
            }
        }


    }


        location @rewrite_installapp {
            rewrite ^(.*)$ /install/app.php/$1 last;
        }


        location @rewriteapp {
            rewrite ^(.*)$ /app.php/$1 last;
        }

    error_page  403 /error/404.html;
    error_page  404 /error/404.html;
    error_page  500 502 503 504 /error/50x.html;

    location /error/ {
        alias   /home/united/web/mysite.net/document_errors/;
    }

    location ~* "/\.(htaccess|htpasswd)$" {
        deny    all;
        return  404;
    }



    include     /etc/nginx/conf.d/phpmyadmin.inc*;
    include     /etc/nginx/conf.d/phppgadmin.inc*;
    include     /etc/nginx/conf.d/webmail.inc*;

    include     /home/united/conf/web/nginx.mysite.net.conf*;
}

However, it just doesn't seem to work. I can access https://www.mysite.net/forum/install/app.php fine - but as soon as I click "install" and go to:

https://www.mysitenet/forum/install/app.php/install

...I get a 404 :(

Any help would be much appreciated. I'm pulling my hair out here :(

Andy

User avatar
EA117
Registered User
Posts: 1391
Joined: Wed Aug 15, 2018 3:23 am
Contact:

Re: setting up with nginx rewrites

Post by EA117 »

I am the worst person to be getting NGINX suggestions from, having never used or configured it before. The little I know I learned from phpBB's example configuration file. 😜

But to my eye, I think there is an extra curly brace closing the block after the location /forum/install/ { ... } block. I don't know exactly what kind of effect this would have on the parsing of the configuration, but it certainly seems possible this might negate or skip the subsequent definition of the location @rewrite_installapp { ... } block that was being referenced as part of redirecting /install folder URLs.

User avatar
AbaddonOrmuz
Recognised Extension Developer
Posts: 820
Joined: Wed Dec 25, 2013 9:06 pm
Location: /dev/null
Name: Alfredo Ramos
Contact:

Re: setting up with nginx rewrites

Post by AbaddonOrmuz »

Aside from the extra curly brace as EA117 mentions, you also need to prepend /forum in try_files and rewrite directives, as you are installing in a subdirectory.

I would suggest to use an updated nginx sample configuration file:

https://github.com/phpbb/phpbb/blob/3.3 ... ample.conf

Code: Select all

	# phpBB
	location /forum/ {
		try_files $uri $uri/ @rewriteapp;

		# Pass the php scripts to FastCGI server specified in upstream declaration.
		location ~ \.php(/|$) {
			include fastcgi.conf;
			fastcgi_split_path_info ^(.+\.php)(/.*)$;
			fastcgi_param PATH_INFO $fastcgi_path_info;
			fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
			fastcgi_param DOCUMENT_ROOT $realpath_root;
			try_files $uri $uri/ /forum/app.php$is_args$args;
			fastcgi_pass php;
		}

		# Deny access to internal phpbb files.
		location ~ /(config\.php|common\.php|cache|files|images/avatars/upload|includes|(?<!ext/)phpbb(?!\w+)|store|vendor) {
			deny all;
			# deny was ignored before 0.8.40 for connections over IPv6.
			# Use internal directive to prohibit access on older versions.
			internal;
		}
	}

	location @rewriteapp {
		rewrite ^(.*)$ /forum/app.php/$1 last;
	}

	# Correctly pass scripts for installer
	location /forum/install/ {
		try_files $uri $uri/ @rewrite_installapp =404;

		# Pass the php scripts to fastcgi server specified in upstream declaration.
		location ~ \.php(/|$) {
			include fastcgi.conf;
			fastcgi_split_path_info ^(.+\.php)(/.*)$;
			fastcgi_param PATH_INFO $fastcgi_path_info;
			fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
			fastcgi_param DOCUMENT_ROOT $realpath_root;
			try_files $uri $uri/ /forum/install/app.php$is_args$args =404;
			fastcgi_pass php;
		}
	}

	location @rewrite_installapp {
		rewrite ^(.*)$ /forum/install/app.php/$1 last;
	}
Note how it's not necessary to prepend with /forum the location for # Deny access to internal phpbb files. and PHP setup, because they're inside the /forum/ or /forum/install/ location.

It works for both phpBB 3.2.x and 3.3.x. I guess it would also work in phpB 3.1.x

Make sure you validate the configuration file with nginx -t
Some of my phpBB extensions:
Image Imgur | :chart_with_upwards_trend: SEO Metadata | Image Markdown | :lock: Auto-lock Topics
:trophy: Check out all my validated extensions :trophy:

:penguin: Arch Linux user :penguin:

youradds
Registered User
Posts: 19
Joined: Wed Jan 02, 2008 12:54 pm

Re: setting up with nginx rewrites

Post by youradds »

Hi,

Thanks - I think I've finally got it:

Code: Select all

server {
    listen      123.123.123.123:443 ssl http2;
    listen      [::]:443 ssl http2;
    server_name mysite.net www.mysite.net;
    root        /home/united/web/mysite.net/public_html;
    index       index.php index.html index.htm;
    access_log  /var/log/nginx/domains/mysite.net.log combined;
    access_log  /var/log/nginx/domains/mysite.net.bytes bytes;
    error_log   /var/log/nginx/domains/mysite.net.error.log error;

    ##ssl         on;
    ssl_certificate      /home/united/conf/web/ssl.mysite.net.pem;
    ssl_certificate_key  /home/united/conf/web/ssl.mysite.net.key;

    location = /favicon.ico {
        log_not_found off;
        access_log off;
    }

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

    location / {

        include     /home/united/conf/web/nginx.mysite.net.rules.conf*;

        #ssi on;

        # location ~* ^.+\.(jpeg|jpg|png|gif|bmp|ico|svg|css|js|eot|otf|ttf|woff|woff2)$ {

        #     rewrite "/2018/v\d+/js/(.+)\.js$" /2018/js/$1.js last;
        #     rewrite "/2018/v\d+/css/(.+)\.css$" /2018/css/$1.css last;
        #     rewrite "/2018/v\d+/js/(.+)\.css$" /2018/js/$1.css last;

        #     expires     max;
        # }


        location ~ \.cgi$ {
            try_files $uri =404;
            gzip off;
            proxy_set_header X-Real-IP  $remote_addr;
            proxy_set_header X-Forwarded-For $remote_addr;
            proxy_set_header Host $host;
            proxy_pass http://127.0.0.1:8181;

        }

        location ~ [^/]\.php(/|$) {
            fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;

            fastcgi_pass    127.0.0.1:9003;
            fastcgi_index   index.php;
            include         /etc/nginx/fastcgi_params;

            # Necessary for php.
            fastcgi_split_path_info ^(.+\.php)(/.*)$;
            fastcgi_param PATH_INFO $fastcgi_path_info;
            fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
            fastcgi_param DOCUMENT_ROOT $realpath_root;
            try_files $uri $uri/ /app.php$is_args$args;


        }


    }


    location /forum/ {
        # phpBB uses index.htm
        index index.php index.html index.htm;
        try_files $uri $uri/ @rewriteapp;
    }

    # Deny access to internal phpbb files.
    location ~ /(config\.php|common\.php|cache|files|images/avatars/upload|includes|(?<!ext/)phpbb(?!\w+)|store|vendor) {
        deny all;
        # deny was ignored before 0.8.40 for connections over IPv6.
        # Use internal directive to prohibit access on older versions.
        internal;
    }

    # Deny access to internal phpbb files.
    location ~ /(config\.php|common\.php|cache|files|images/avatars/upload|includes|(?<!ext/)phpbb(?!\w+)|store|vendor) {
        deny all;
        # deny was ignored before 0.8.40 for connections over IPv6.
        # Use internal directive to prohibit access on older versions.
        internal;
    }



    # Correctly pass scripts for installer
    location /forum/install/ {
        # phpBB uses index.htm
        try_files $uri $uri/ @rewrite_installapp =404;

        # Pass the php scripts to fastcgi server specified in upstream declaration.
        location ~ \.php(/|$) {
            # Unmodified fastcgi_params from nginx distribution.
            include         /etc/nginx/fastcgi_params;
            # Necessary for php.
            fastcgi_split_path_info ^(.+\.php)(/.*)$;
            fastcgi_param PATH_INFO $fastcgi_path_info;
            fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
            fastcgi_param DOCUMENT_ROOT $realpath_root;
            try_files $uri $uri/ /forum/install/app.php$is_args$args =404;
            fastcgi_pass    127.0.0.1:9003;
        }
    }

    location @rewrite_installapp {
        rewrite ^(.*)$ /install/app.php/$1 last;
    }


    location @rewriteapp {
        rewrite ^(.*)$ /app.php/$1 last;
    }

    error_page  403 /error/404.html;
    error_page  404 /error/404.html;
    error_page  500 502 503 504 /error/50x.html;

    location /error/ {
        alias   /home/united/web/mysite.net/document_errors/;
    }

    location ~* "/\.(htaccess|htpasswd)$" {
        deny    all;
        return  404;
    }



    include     /etc/nginx/conf.d/phpmyadmin.inc*;
    include     /etc/nginx/conf.d/phppgadmin.inc*;
    include     /etc/nginx/conf.d/webmail.inc*;

    include     /home/united/conf/web/nginx.mysite.net.conf*;
}
It restarts now, and I can get to app.php/install :) I'll give the rest of it a go tomorrow with a fresh mind

Thanks guys!

Post Reply

Return to “[3.3.x] Support Forum”