nginx config for phpbb and laravel - laravel

I need an nginx config that will work for both Laravel and Phpbb.
I have used laravel forge to setup my digital ocean server, and it created this nginx config:
# FORGE CONFIG (DOT NOT REMOVE!)
include forge-conf/djembefola.org/before/*;
server {
listen 80;
listen [::]:80;
server_name djembefola.org;
root /home/forge/djembefola.org/public;
# FORGE SSL (DO NOT REMOVE!)
# ssl_certificate;
# ssl_certificate_key;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_ciphers 'ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-AES256-GCM-SHA384:DHE-RSA-AES128-GCM-SHA256:DHE-DSS-AES128-GCM-SHA256:kEDH+AESGCM:$
ssl_prefer_server_ciphers on;
ssl_dhparam /etc/nginx/dhparams.pem;
add_header X-Frame-Options "SAMEORIGIN";
add_header X-XSS-Protection "1; mode=block";
add_header X-Content-Type-Options "nosniff";
index index.html index.htm index.php;
charset utf-8;
# FORGE CONFIG (DOT NOT REMOVE!)
include forge-conf/djembefola.org/server/*;
location / {
try_files $uri $uri/ /index.php?$query_string;
}
location = /favicon.ico { access_log off; log_not_found off; }
location = /robots.txt { access_log off; log_not_found off; }
access_log off;
error_log /var/log/nginx/djembefola.org-error.log error;
error_page 404 /index.php;
location ~ \.php$ {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;
fastcgi_index index.php;
include fastcgi_params;
}
location ~ /\.(?!well-known).* {
deny all;
}
}
# FORGE CONFIG (DOT NOT REMOVE!)
include forge-conf/djembefola.org/after/*;
The /public folder is where the front controller index.php of Laravel lives...
Also in the public folder, I have and install of phpbb at
- /public/board
I am upgrading the forum, and as such I need to rum the phpbb installer, which resides at:
localhost/board/install,
which then calls:
localhost/board/install/app.php/update
The above url is then giving a 404 error.
I have read elsewhere that this is because Nginx needs to be configured correctly in order to run the installer.
The sample Nginx config for phpbb is listed here.
So I need to merge these somehow, but so far my attempts have failed.
I tried adding :
location /board/ {
rewrite ^(.*)$ /app.php/$1 last;
}
to the existing laravel nginx file, but that fails. I am aware that I need to put it in the right place in the nginx config, but I fear I'm probably overlooking something else, as I am guessing a bit here...
Can anyone help please?

Based off the example config, you can nest required rules within a block wrap (just like they have done with the /install/ directory). eg:
location /board/ {
try_files $uri $uri/ #rewriteapp;
# Deny access to internal phpbb files.
location ~ /(config\.php|common\.php|cache|files|images/avatars/upload|includes|(?<!ext/)phpbb|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;
}
# 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/ /board/app.php$is_args$args;
fastcgi_pass php;
}
}
location #rewriteapp {
rewrite ^(.*)$ /app.php/$1 last;
}
Note that #rewriteapp is outside the block wrap. Nginx will complain if you try to place it inside.
Just amend the paths to match your directory.

Related

Setting up multisite with nginx

Having some problems setting up the following setup with nginx:
example.com
example.com/gb/en
These are laravel sites and the gb/en is the first of more that will be added. Root works obvs but I can't get /gb/en to point at the /gb/en/public folder.
I have tried a bunch of things but this is my current conf file:
server {
listen 80;
listen [::]:80;
server_name www.example.com;
root /home/forge/www.example.com/public;
ssl_protocols TLSv1.2;
ssl_ciphers TEST;
ssl_prefer_server_ciphers on;
ssl_dhparam /etc/nginx/dhparams.pem;
add_header X-Frame-Options "SAMEORIGIN";
add_header X-XSS-Protection "1; mode=block";
add_header X-Content-Type-Options "nosniff";
index index.html index.htm index.php;
charset utf-8;
location / {
try_files $uri $uri/ /index.php?$query_string;
}
location /gb/en {
root /home/forge/www.example.com/gb/en/public;
}
location = /favicon.ico { access_log off; log_not_found off; }
location = /robots.txt { access_log off; log_not_found off; }
access_log off;
error_log /var/log/nginx/www.example.com-error.log error;
error_page 404 /index.php;
location ~ \.php$ {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php/php7.1-fpm.sock;
fastcgi_index index.php;
include fastcgi_params;
}
location ~ /\.(?!well-known).* {
deny all;
}
}
You can see my location block in there which I have tried a number of different ways but to no avail.
Any assistance would be greatly appreciated.
EDIT:
So I've got one step closer once I realised that the sub folders in the url also get added to the root directory:
location /gb/en/ {
root /home/forge/www.example.com/public;
try_files $uri $uri/ /public/index.php?url=$uri;
}
So, my files are under /public/gb/en/public but the above resolves to /public/gb/en/index.php
How can I get /gb/en/ to resolve to public/gb/en/public/index.php?
Many thanks ;)
EDIT:
The idea came to me suddenly - use a symlink! I symlinked the /gb/en to the public folder in a separate folder and it worked a treat ;)
I would sugest to copy the try_files line into the /en/gb block. In that folder or site you are not specifying the extensions you would like to use as you are doing in the one that works.
Try this:
location /gb/en {
root /home/forge/www.example.com/gb/en/public;
try_files $uri $uri/ /index.php?$query_string;
}
However, if the content of /en/gb is related to the site, setting this up directly in Laravel might be a better option.
I used a symlink in the end to achieve this. I symlinked /gb/en to the public folder in another location and it did what I was after ;)
EDIT - I later found that this also had problems with sub pages but then found this example and it helped to achieve what I wanted to in the first place https://gist.github.com/noeldiaz/08a1211a7c47f21f7083

Rewrite laravel query parameters with nginx

I have a laravel web app that acts as a CMS and has several sites in one app. I access these sites with the following url:
my-site.com/site/1 - Leads to site 1
I want to rewrite NGINX so that i can point several top domains to their respective site in my CMS.
my-site-1.com -> my-site.com/site/1
How can this be achieved? All questions i can find wants to point a domain to a subfolder, i want to point it to a query parameter.
I use Larave forge + Digitalocean. This is my configuration file
# FORGE CONFIG (DOT NOT REMOVE!)
include forge-conf/default/before/*; #*/
server {
listen 80 default_server;
server_name default;
root /home/forge/default/public;
# FORGE SSL (DO NOT REMOVE!)
# ssl_certificate;
# ssl_certificate_key;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
index index.html index.htm index.php;
charset utf-8;
# FORGE CONFIG (DOT NOT REMOVE!)
include forge-conf/default/server/*; #*/
location / {
try_files $uri $uri/ /index.php?$query_string;
if ($http_host ~ "^(www.)?my-site-1.com$"){
#I tried adding som conditional rules here
}
}
location = /favicon.ico { access_log off; log_not_found off; }
location = /robots.txt { access_log off; log_not_found off; }
access_log off;
error_log /var/log/nginx/default-error.log error;
error_page 404 /index.php;
location ~ \.php$ {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_connect_timeout 18000;
fastcgi_send_timeout 18000;
fastcgi_read_timeout 18000;
}
location ~ /\.ht {
deny all;
}
}
# FORGE CONFIG (DOT NOT REMOVE!)
include forge-conf/default/after/*; #*/

Nginx / Laravel config to redirect from non index.php route

I've just deployed a newly built website running Laravel 5 and nginx and struggling to setup my nginx config to 301 redirect some old URLs - specifically those with .php extensions.
I'm looking to 301 redirect the below...
domain.com/foobar.php?id=123 -> domain.com/foobar/123
The default config assumes everything runs from index.php, so Laravel does not trigger on foobar.php.
I have tried adding the following rewrite rule, but it doesn't trigger (I assume because nginx isn't listening out for foobar.php requests?
rewrite ^/foobar.php(.*)$ /foobar/$1/ permanent;
Relevant snippet of my current nginx config below...
server {
listen 443 ssl;
server_name domain.com;
root /home/domain.com/public;
index index.html index.htm index.php;
charset utf-8;
if ($request_method = GET ) {
rewrite ^([^.]*[^/])$ $1/ permanent;
}
location / {
try_files $uri $uri/ /index.php?$query_string;
}
location = /favicon.ico { access_log off; log_not_found off; }
access_log off;
error_log /var/log/nginx/domain.com-error.log error;
error_page 404 /index.php;
location ~ \.php$ {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
include fastcgi_params;
}
location ~ /\.ht {
deny all;
}
}
This should work:
rewrite ^/foobar.php /foobar/$arg_id/? permanent;
You can get any argument like this $arg_{your_argument}. Just replace {your_argument} with the actual argument name.

Magento Nginx sub-page rewrites

I am in the process of switching from Apache to Nginx for Magento but cannot seem to get it set up and working correctly. I have installed everything on a test server and the magento install went fine and I can access both the home page and the admin panel correctly but if I navigate to any sub-page from the frontend, the URL loses the "index.php" and gives me a 500 internal server error. For example if I try /magento/contacts/ I get a 500 error but if I add /magento/index.php/contacts/ it loads.
I have tried countless different nginx configurations but none have worked. I have trawled the posts on here relating to the same issue but no joy. My last hope is to post my config file on here and hope that someone can help!
I followed this: https://gist.github.com/tegansnyder/96d1be1dd65852d3e576 tutorial to the letter and everything works great except the problem above.
Any suggestions would be GREATLY appreciated! I'm tearing my hair out here! Thanks in advance!
I have two config files:
FIRST CONFIG FILE
server {
server_name 192.121.166.136;
access_log /var/log/nginx/access.log main;
error_log /var/log/nginx/error.log info;
# 504 is a PHP timeout and must be static
# 502 is momentary during a PHP restart and should be treated like maintenance
# other 50x errors are handled by Magento
error_page 502 504 /var/www/magento/504.html;
listen 80;
#listen 443 ssl;
# if you are using a load balancer uncomment these lines
# header from the hardware load balancers
#real_ip_header X-Forwarded-For;
# trust this header from anything inside the subnet
#set_real_ip_from X.X.X.1/24;
# the header is a comma-separated list; the left-most IP is the end user
#real_ip_recursive on;
# ensure zero calls are written to disk
client_max_body_size 16m;
client_body_buffer_size 2m;
client_header_buffer_size 16k;
large_client_header_buffers 8 8k;
root /var/www/;
index index.php;
fastcgi_read_timeout 90s;
fastcgi_send_timeout 60s;
# ensure zero calls are written to disk
fastcgi_buffers 512 16k;
fastcgi_buffer_size 512k;
fastcgi_busy_buffers_size 512k;
# remove the cache-busting timestamp
location ~* (.+)\.(\d+)\.(js|css|png|jpg|jpeg|gif)$ {
try_files $uri $1.$3;
access_log off;
log_not_found off;
expires 21d;
add_header Cache-Control "public";
}
# do not log static files; regexp should capture alternate cache-busting timestamps
location ~* \.(jpg|jpeg|gif|css|png|js|ico|txt|swf|xml|svg|svgz|mp4|ogg|ogv)(\?[0-9]+)?$ {
access_log off;
log_not_found off;
expires 21d;
add_header Cache-Control "public";
}
# Server
include main.conf;
include security.conf;
}
SECOND CONFIG FILE
rewrite_log on;
location / {
index index.php;
try_files $uri $uri/ #handler;
}
location #handler {
rewrite / /index.php;
}
## force www in the URL
if ($host !~* ^www\.) {
#rewrite / $scheme://www.$host$request_uri permanent;
}
## Forward paths like /js/index.php/x.js to relevant handler
location ~ \.php/ {
rewrite ^(.*\.php)/ $1 last;
}
location /media/catalog/ {
expires 1y;
log_not_found off;
access_log off;
}
location /skin/ {
expires 1y;
}
location /js/ {
access_log off;
}
location ~ \.php$ { ## Execute PHP scripts
if (!-e $request_filename) { rewrite / /index.php last; } ## Catch 404s that try_files miss
expires off; ## Do not cache dynamic content
# for this tutorial we are going to use a unix socket
# but if HHVM was running on another host we could forego unix socket
# in favor of an IP address and port number as follows:
#fastcgi_pass 127.0.0.1:8080;
fastcgi_pass unix:/var/run/hhvm/sock;
fastcgi_index index.php;
#fastcgi_param HTTPS $fastcgi_https;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
# if you need to explictly specify a store code for Magento do it here
# this is useful if you are running multiple stores with different hostnames
#fastcgi_param MAGE_RUN_CODE default;
#fastcgi_param MAGE_RUN_TYPE store;
include fastcgi_params; ## See /etc/nginx/fastcgi_params
fastcgi_keep_conn on; #hhvm param
}
The problem was that my store was in /var/www/magento. Moved the files to just /var/www and all is good :) Thank for the help guys.
Please have a complete reading of this document: http://info.magento.com/rs/magentocommerce/images/MagentoECG-PoweringMagentowithNgnixandPHP-FPM.pdf where you can find a basic NGINX configuration specifically for Magento:
server {
listen 80 default;
# like ServerName in Apache
server_name magento.lan www.magento.lan;
# document root, path to directory with files
root /var/www/magento;
index index.html index.php;
# we don’t want users to see files in directories
autoindex off;
location ~ (^/(app/\|includes/\|lib/\|/pkginfo/\|var/\|report/config.
xml)\|/\.svn/\|/\.git/\|/.hta.+) {
#ensure sensitive files are not accessible
deny all;
}
location / {
# make index.php handle requests for /
try_files $uri $uri/ /index.php?$args;
# do not log access to static files
access_log off;
# cache static files aggressively
expires max;
}
location \~\* \.(jpeg\|jpg\|gif\|png\|css\|js\|ico\|swf)$ {
# look for static files in root directory
# and ask backend if not successful
try_files $uri $uri/ #proxy;
expires max;
access_log off;
}
location #proxy {
# proxy everything from this location to backend
fastcgi_pass fpm_backend;
}
location \~\.php$ {
# if reference to php executable is invalid return 404
try_files $uri =404;
# no need to cache php executable files
expires off;
fastcgi_read_timeout 600;
# proxy all requests for dynamic content to
fastcgi_pass fpm_backend;
# backend configured in upstream.conf
fastcgi_keep_conn on; # use persistent connects to backend
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root${fastcgi_script_name};
# Store code is defined in
# administration > Configuration > Manage Stores
fastcgi_param MAGE_RUN_CODE default;
fastcgi_param MAGE_RUN_TYPE store;
}
}

Nginx/PHP-FPM: Returning 404s for CSS and JS

I can't access PHP files placed in sub-directories and my CSS/JS files return a 404 in browser. Yes, I have seen many others experiencing a 404 issue but their fixes don't seem to be my solution. I've also swapped out my configuration with several online. Here is my configuration:
server {
# Port that the web server will listen on.
listen 80;
# Host that will serve this project.
server_name .stackoverflow.com;
# Useful logs for debug.
access_log /var/log/nginx/access.log;
error_log /var/log/nginx/error.log;
rewrite_log on;
# The location of our projects public directory.
root /usr/share/nginx/html/public;
# Point index to the Laravel front controller.
index index.php;
location / {
# URLs to attempt, including pretty ones.
try_files $uri $uri/ /index.php?$query_string;
}
# Remove trailing slash to please routing system.
if (!-d $request_filename) {
rewrite ^/(.+)/$ /$1 permanent;
}
# PHP FPM configuration.
location ~* \.php$ {
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
fastcgi_split_path_info ^(.+\.php)(.*)$;
include /etc/nginx/fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
# We don't need .ht files with nginx.
location ~ /\.ht {
deny all;
}
# Set header expirations on per-project basis
location ~* \.(?:ico|css|js|jpe?g|JPG|png|svg|woff)$ {
expires 365d;
}
}
Is there something wrong with my configuration?

Resources