I setup my vagrant box via a pre-configured https://puphpet.com/ package with PHP 5.4 and Nginx.
After provisioning, I always have to change the vhost_autogen.conf by myself - I guess that cannot be the idea behind vagrant and puppet :-) How can I:
remove the vhost_autogen.conf automatically and replace it with another one
OR
add the settings below as puppet configuration?
This is the required vhost setting:
server {
listen *:80;
server_name example.dev www.example.dev;
root /var/www/example.com;
index index.php;
access_log /var/log/nginx/example.com.de.access.log;
error_log /var/log/nginx/example.com.error.log;
if ($host ~* www.(.*)) {
set $host_without_www $1;
rewrite ^(.*)$ $scheme://$host_without_www$1 permanent; #1
#rewrite ^ $scheme://$host_without_www$1request_uri permanent; #2
}
# Rewrite for minify
rewrite ^/min/([a-z]=.*) /min/index.php?$1 last;
# canonicalize codeigniter url end points
if ($request_uri ~* ^(/start(/index)?|/index(.php)?)/?$)
{
rewrite ^(.*)$ / permanent;
}
# removes trailing "index" from all controllers
if ($request_uri ~* index/?$)
{
rewrite ^/(.*)/index/?$ /$1 permanent;
}
# removes trailing slashes (prevents SEO duplicate content issues)
if (!-d $request_filename)
{
rewrite ^/(.+)/$ /$1 permanent;
}
# removes access to "system" folder, also allows a "System.php" controller
if ($request_uri ~* ^/system)
{
rewrite ^/(.*)$ /index.php?/$1 last;
break;
}
# unless the request is for a valid file (image, js, css, etc.), send to bootstrap
if (!-e $request_filename)
{
rewrite ^/(.*)$ /index.php?/$1 last;
break;
}
# catch all
error_page 404 /index.php;
location / {
root /var/www/example.com;
rewrite ^/min/([a-z]=.*) /min/index.php?$1 last;
}
location = /robots.txt { access_log off; log_not_found off; }
location = /favicon.ico { access_log off; log_not_found off; }
location ~ /. { access_log off; log_not_found off; deny all; }
location ~ ~$ { access_log off; log_not_found off; deny all; }
location ~ .php$ {
root /var/www/example.com;
try_files $uri $uri/ /index.php?$args ;
index index.html index.htm index.php;
fastcgi_index index.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;
fastcgi_param APP_ENV dev;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
include fastcgi_params;
}
}
In puppet/manifest.pp, search for create_resources(nginx_vhost, $nginx_values['vhosts']), replace it with the following:
file { "/etc/nginx/sites-enabled/my-site.conf":
source => '/full/path/to/conf/file',
owner => 'root',
group => 'root',
mode => '0644'
}
Related
Having an issue on wordpress multisite setup with nginx, it is redirecting over and over again with the same https/url on subfolder, but main site is working. Can someone please help me? This is my configuration. Thanks in advance.
Take note: Server is EC2 instance under Application Load Balancer and Cloudfront
server {
server_name _;
listen 80 reuseport;
root /var/www/html;
index index.php;
set $upstream_endpoint ${FPM_SERVER};
set $proxy_https '';
set $csp '';
if ($http_cloudfront_forwarded_proto = 'https') {
set $proxy_https 'on';
}
if ($http_x_forwarded_proto = 'https') {
set $proxy_https 'on';
}
if ($scheme = 'https') {
set $proxy_https 'on';
}
if ($proxy_https = 'on'){
set $csp 'upgrade-insecure-requests;';
}
add_header Content-Security-Policy $csp;
location = /favicon.ico {
allow all;
log_not_found off;
access_log off;
}
location = /robots.txt {
allow all;
log_not_found off;
access_log off;
}
location / {
# try to serve file directly, fallback to index.php
# try_files $uri $uri/ /index.php$args;
try_files $uri $uri/ /index.php?q=$uri&$args;
proxy_set_header Host $host;
}
### ADDED CONFIG
###
# rewrite /wp-admin$ $scheme://$host$uri/ permanent;
location ~* \.(js|css|png|jpg|jpeg|gif|ico)$ {
expires max;
log_not_found off;
}
location /blogs {
try_files $uri $uri/ /blogs/index.php?q=$uri&$args;
}
location /faq {
try_files $uri $uri/ /faq/index.php?q=$uri&$args;
}
rewrite ^/files/(.+) /wp-includes/ms-files.php?file=$1 last;
location ^~ /files/ {
rewrite ^.*/files/(.+)$ /wp-includes/ms-files.php?file=$1 last;
}
# Rewrite multisite '.../wp-.*' and '.../*.php'.
if (!-e $request_filename) {
rewrite /wp-admin$ $scheme://$host$uri/ permanent;
rewrite ^/[_0-9a-zA-Z-]+(/wp-.*) $1 last;
break;
rewrite ^/[_0-9a-zA-Z-]+(/.*\.php)$ $1 last;
}
location ~ \.php$ {
include fastcgi_params;
fastcgi_index index.php;
fastcgi_intercept_errors on;
fastcgi_split_path_info ^(.+\.php)(/.*)$;
fastcgi_param SCRIPT_FILENAME /var/www/html$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info;
fastcgi_param HTTPS $proxy_https if_not_empty;
fastcgi_param REMOTE_ADDR $proxy_add_x_forwarded_for;
fastcgi_pass $upstream_endpoint;
}
# return 404 for all other php files not matching the front controller
# this prevents access to other php files you don't want to be accessible.
location ~ \.php$ {
return 404;
}
}
This is my current nginx configuration for a special host:
server {
listen *:80;
server_name example.test;
root /var/www/example;
index index.php index.htm index.html;
access_log /var/log/nginx/example.access.log;
error_log /var/log/nginx/example.error.log;
if ($host ~* www\.(.*)) {
set $host_without_www $1;
rewrite ^(.*)$ $scheme://$host_without_www$1 permanent; #1
#rewrite ^ $scheme://$host_without_www$1request_uri permanent; #2
}
# Rewrite for minify
rewrite ^/min/([a-z]=.*) /min/index.php?$1 last;
# canonicalize codeigniter url end points
# if your default controller is something other than "welcome" you should change the following
if ($request_uri ~* ^(/start(/index)?|/index(.php)?)/?$)
{
rewrite ^(.*)$ / permanent;
}
# removes trailing "index" from all controllers
if ($request_uri ~* index/?$)
{
rewrite ^/(.*)/index/?$ /$1 permanent;
}
# removes trailing slashes (prevents SEO duplicate content issues)
if (!-d $request_filename)
{
rewrite ^/(.+)/$ /$1 permanent;
}
# removes access to "system" folder, also allows a "System.php" controller
if ($request_uri ~* ^/system)
{
rewrite ^/(.*)$ /index.php?/$1 last;
break;
}
# unless the request is for a valid file (image, js, css, etc.), send to bootstrap
if (!-e $request_filename)
{
rewrite ^/(.*)$ /index.php?/$1 last;
break;
}
# catch all
error_page 404 /index.php;
location / {
root /var/www/example;
rewrite ^/min/([a-z]=.*) /min/index.php?$1 last;
}
location = /robots.txt { access_log off; log_not_found off; }
location = /favicon.ico { access_log off; log_not_found off; }
location ~ /\. { access_log off; log_not_found off; deny all; }
location ~ ~$ { access_log off; log_not_found off; deny all; }
location ~ \.php$ {
root /var/www/example;
try_files $uri $uri/ /index.php?$args ;
index index.html index.htm index.php;
fastcgi_index index.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;
fastcgi_param APP_ENV dev;
fastcgi_pass docker-php-fpm:9000;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
include fastcgi_params;
}
}
My distribution files are in /var/www/example/templates/dist/, but I want to rewrite requests for /dist/ internally to the /templates/dist/ folder.
I tried something like this:
location /dist {
root /var/www/example/templates/dist;
}
without success. I always get a 404 by my index.php
It will be no rocket science I guess. Can someone help me with that? :)
solution found:
location ~ ^/dist/(.*)$ {
try_files $uri $uri/ /templates/dist/$1;
}
I had three WordPress Installations /basketball /football and /kitesurfen each was defined in nginx.conf file with separate directories.
removed all configurations and installed a multisite on / path.
when I create new site with one of the previous paths e.g /basketball /football or /kitesurfen I can not access the dashboard. I get ERR_TOO_MANY_REDIRECTS and different error based on browser.
If I create a site with any other path e.g /test /volleyball or /swimming it works fine.
Need help to fix this. I am not sure if its a WordPress (PHP) error or Nginx configurations error.
Nginx Conf
if (!-e $request_filename) {
rewrite ^/([_0-9a-zA-Z-]+/)?(wp-(content|admin|includes).*) /$2 break;
}
location / {
root /var/www/html/multinetworks/networktheme;
index index.html index.htm index.php;
rewrite ^/([_0-9a-zA-Z-]+/)?wp-admin$ /$1wp-admin/ redirect;
rewrite ^/([_0-9a-zA-Z-]+/)?(.*\.php)$ /$2 break;
rewrite ^/([_0-9a-zA-Z-]+/)?(.*\.php)$ /$2 break;
try_files $uri $uri/ /index.php$is_args$args;
location = /favicon.ico {
log_not_found off; access_log off;
}
location = /robots.txt {
log_not_found off; access_log off; allow all;
}
location ~* \.(css|gif|ico|jpeg|jpg|js|png)$ {
expires max;
log_not_found off;
}
location ~ \.php$ {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
fastcgi_index index.php;
}
}
I'm having a main domain example.com and an example.com/admin/. This admin domain has a different document root and will render the admin interface. The problem currently is that the file is server plain-text by nginx. So I can basically see the index.php file. I'm trying to figure it out, but no success yet.
This is my nginx config:
server {
listen 127.0.0.1:8080;
server_name www.example.me;
rewrite ^(.*) http://example.me$1 permanent;
}
server {
listen 127.0.0.1:8080;
server_name example.me;
root /var/www/example.me/laravel/example/public/;
index index.html index.htm index.php;
error_log /var/log/nginx/example.me.error.log error;
access_log /var/log/nginx/example.me.access.log;
port_in_redirect off;
merge_slashes on;
client_max_body_size 20M;
error_page 404 =301 http://example.me;
location / {
#Don't use slash at end
rewrite ^/(.*)/$ /$1 permanent;
# add rewrite rule here:
# block access to /index.(php|htm|html)
if ($request_uri ~ "/index.(php|html?)") {
rewrite ^ /$1 permanent;
}
if (!-e $request_filename) {
rewrite ^.*$ /index.php last;
}
}
location ~ \.php$ {
include /etc/nginx/fastcgi_params;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_intercept_errors on;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
location ^~ /admin {
root /var/www/example.me/zend/public/;
index index.php;
try_files /index.php$is_args$args $uri;
auth_basic "example Admin";
auth_basic_user_file /var/www/example.me/zend/public/.htpasswd;
rewrite_log on;
access_log /var/log/nginx/adminexample.access.log;
error_log /var/log/nginx/adminexample.error.log notice;
location ~ \.php$ {
include fastcgi_params;
fastcgi_pass 127.0.0.1:9000;
#fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
#fastcgi_param SCRIPT_FILENAME /var/www/example.me/zend/public$fastcgi_script_name;
}
}
}
I have installed a Magento extension to have a wordpress blog integrated with Magento.
Basically, the WP is in a subdirectory of the Magento root. I want to create multiple sites with subdirectories but I can't make it work due to the nginx configuration.
Wordpress is in his /wp subdirectory (http://example.com/wp/wp-admin/) and the others sites are accessible from http://example.com/wp/ca/wp-admin/ and http://example.com/wp/en/wp-admin/
Here is whats I got so far :
server
{
server_name dev.example.com;
access_log /var/log/nginx/example.access.log;-
error_log /var/log/nginx/example.error.log;
root /var/www/example;
location ^~ /wp {
index index.php index.html index.htm;
try_files $uri $uri/ /wp/index.php?q=$uri&$args;
# Multisite
if (!-e $request_filename) {
rewrite /wp-admin$ $scheme://$host$uri/ permanent;
rewrite ^/wp(/[^/]+)?(/wp-.*) /wp$2 last;
rewrite ^/wp(/[^/]+)?(/.*\.php)$ /wp$2 last;
}
location ~ \.php$ {
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $request_filename;
fastcgi_pass 127.0.0.1:9000;
}
}
set $mage_developer true;
set $mage_code es;
set $mage_type store;
include snippets.d/magento-site;-
}
and in snippets.d/magento-site :
# Serve static pages directly,
# otherwise pass the URI to Magento's front handler
location / {
index index.php;
try_files $uri $uri/ #handler;
expires 30d;-
}
# Disable .htaccess and other hidden files
location /. {
return 404;
}
# Allow admins only to view export folder
location /var/export/ {
auth_basic "Restricted";
auth_basic_user_file htpasswd;
autoindex on;
}
# These locations would be hidden by .htaccess normally
location /app/ { deny all; }
location /includes/ { deny all; }
location /lib/ { deny all; }
location /media/downloadable/ { deny all; }
location /pkginfo/ { deny all; }
location /report/config.xml { deny all; }
location /var/ { deny all; }
# Magento uses a common front handler
location #handler {
rewrite / /index.php;
}
# Forward paths like /js/index.php/x.js to relevant handler
location ~ .php/ {
rewrite ^(.*.php)/ $1 last;
}
# Execute PHP scripts
location ~ .php$ {
# Catch 404s that try_files miss
if (!-e $request_filename) { rewrite / /index.php last; }
expires off;
fastcgi_pass 127.0.0.1:9000;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
fastcgi_param MAGE_RUN_CODE $mage_code;
fastcgi_param MAGE_RUN_TYPE $mage_type;
fastcgi_ignore_client_abort on;
fastcgi_read_timeout 900s; # 15 minutes
}
Thanks for your help.
Wanted to pass along a full conf file for anyone who needs to configure this. Please keep in mind, many file paths are unique your your server configuration.
Please note, you'll need to adjust the following parameters based on file paths on your server:
server_name domain.com www.domain.com;
ssl_certificate /sslpath/domain.com.crt;
ssl_certificate_key /sslpath/domain.com.key;
root /webrootpath/domain.com;
rewrite ^/blogpath(.*) /blogpath/index.php?q=$1;
location ^~ /blogpath {
error_log /data/log/nginx/domain.com_error.log;
access_log /data/log/nginx/domain.com_access.log;
Here is the full nginx conf file:
server {
listen 80;
server_name domain.com www.domain.com;
rewrite ^ https://$server_name$request_uri? permanent;
}
server {
listen 443;
server_name domain.com www.domain.com;
ssl on;
ssl_certificate /sslpath/domain.com.crt;
ssl_certificate_key /sslpath/domain.com.key;
ssl_session_timeout 30m;
root /webrootpath/domain.com;
index index.php;
location / {
index index.html index.php;
try_files $uri $uri/ #handler;
expires 30d;
}
location #wp {
rewrite ^/blogpath(.*) /blogpath/index.php?q=$1;
}
location ^~ /blogpath {
root /webrootpath/domain.com;
index index.php index.html index.htm;
try_files $uri $uri/ #wp;
location ~ \.php$ {
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $request_filename;
fastcgi_pass 127.0.0.1:9000;
}
}
location ~ ^/(app|includes|lib|media/downloadable|pkginfo|report/config.xml|var)/ { internal; }
location /var/export/ { internal; }
location /. { return 404; }
location #handler { rewrite / /index.php; }
location ~* .php/ { rewrite ^(.*.php)/ $1 last; }
location ~* .php$ {
if (!-e $request_filename) { rewrite / /index.php last; }
expires off;
fastcgi_pass 127.0.0.1:9000;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param MAGE_RUN_CODE default;
fastcgi_param MAGE_RUN_TYPE store;
include fastcgi_params;
}
error_log /data/log/nginx/domain.com_error.log;
access_log /data/log/nginx/domain.com_access.log;
}
Well, in the end, it works passing all request to the blog to Apache and creating the site in the virtual hosts corresponding.
location ~ ^/blog {
proxy_pass http://apache:80;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_read_timeout 6000s;
}
If someone succeed to make it work with Nginx only, I'm looking forward to his answer :)
Why run Apache? Doesn't make sense to run 2 webservers.
Try adding this to your nginx conf.
location #wp {
rewrite ^/wp(.*) /wp/index.php?q=$1;
}
location ^~ /wp {
root /var/www/example;
index index.php index.html index.htm;
try_files $uri $uri/ #wp;
location ~ \.php$ {
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $request_filename;
fastcgi_pass 127.0.0.1:9000;
}
}