I have having a great deal of difficulty configuring nginx to serve both Xenforo and MODx from the one domain. I have tried hundreds of adjustments, read everything I can and almost have it working correctly... but no cigar.
The crux of the issue appears to be the MODx rewrite hijacks my xenforo /xf/ location and no /xf/ URL's will resolve whatsoever, instead they trigger the MODx default 404 error page.
Full server block is;
server {
## HTTPS server essentials
listen 443;
server_name www.mysite.com.au;
## Common Server block SSL config
include "/opt/bitnami/nginx/conf/bitnami/ssl-apps.conf";
## Xenforo location
location ^~ /xf/ {
root /opt/bitnami/apps/xenforo;
## SEO friendly urls
try_files $uri $uri/ /xf/index.php?$uri&$args;
location ~ \.(php)$ {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_read_timeout 300;
fastcgi_pass unix:/opt/bitnami/php/var/run/www.sock;
fastcgi_index index.php;
fastcgi_ignore_client_abort on;
fastcgi_param SCRIPT_FILENAME $request_filename;
include fastcgi_params;
}
}
## MODx
root /opt/bitnami/apps/mysite.com.au/htdocs;
try_files $uri $uri/ ;
location ~ \.(php|html)$ {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_read_timeout 300;
fastcgi_pass unix:/opt/bitnami/php/var/run/www.sock;
fastcgi_index index.php;
fastcgi_ignore_client_abort on;
fastcgi_param SCRIPT_FILENAME $request_filename;
include fastcgi_params;
}
if (!-f $request_filename) {
rewrite ^/(.*)$ /index.php?q=$1 last;
}
}
HOWEVER, if I change the MODx regex rewrite to;
^/(.*).html$
Then 100% of the xenforo URI's resolve and everything nearly works except MODx directory uris without trailing /index.html do not resolve (ie www.mysite.com.au/content1/ will fail).
I have studied the labyrinth of cascading priority and committed to test at least 80 or more variations over the last 10 hours. I can not get the /xf/ path to live nice with the MODx rewrite no matter what I try.
I need the directories paths to resolve for a bunch of reasons, not least of which is years of SEO work.
Any advise appreciated
got it working with;
rewrite ^/(.*).html$ /index.php?q=$1 last;
Related
I have a Laravel 8 project running on an Nginx Webserver in a Docker container through an Nginx Reverse Proxy.
So DNS -> Reverse Proxy -> Docker Webserver container -> PHP-FPM container
The problem is that when I use Laravel's route() it is missing the www resulting in login not working (as well as assets etc).
Because I AM on the www domain (because of Nginx redirect) but the route endpoint is non-www (so a different site, token issue I believe).
In my .env I have APP_URL set which I believe is only for CLI commands and additionally I have ASSET_URL set, which works for the asset() functions. I should not have to use ASSET_URL if the source of the problem, my setup, is correct (which it clearly is not).
I CAN bypass the problem by using URL::forceRootUrl(config('app.url')); in my router, that forces the APP_URL set in my .env to be used in the RouteUrlGenerator (I believe)
Output of \URL::to('/') is https://example.com, so NON-WWW (obv. WITHOUT using URL::forceRootUrl(config('app.url'));)
Even though my browser's URL is including www, so https://www.example.com/blabla.
I would say something is wrong in my setup, either my DNS, Nginx Reverse Proxy, or Nginx Webserver configs but I can't figure out what it is...
I hope it is just a simple redirect fix somehow.
I have also already restarted my servers etc and done a php artisan optimize:clear and also config:cache and config:clear to be sure.
Please find below my configs:
DNS via CloudFlare (proxied):
A example.com 123.123.123.123 Proxied
A www 123.123.123.123 Proxied
CNAME * example.com DNS Only
My Nginx Reverse Proxy config:
server {
listen 80;
listen [::]:80;
server_name example.com www.example.com;
location / {
proxy_pass http://localhost:8012;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Host $server_name;
}
}
My Nginx webserver config:
server {
# listen 80;
server_name example.com;
return 301 https://www.example.com$request_uri;
}
server {
# listen 80 default_server;
index index.html index.htm index.php;
server_name www.example.com;
root /var/www/public;
location ~ \.php$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass php-fpm:9012;
fastcgi_index index.php;
include fastcgi_params;
# fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param SCRIPT_FILENAME /var/www/example.com/public/$fastcgi_script_name;
# fastcgi_param SCRIPT_FILENAME $document_root/example.com.au/$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info;
}
# Support Clean (aka Search Engine Friendly) URLs & enable Gzip compression:
location / {
try_files $uri $uri/ /index.php?$query_string;
gzip_static on;
}
error_log /var/log/nginx/error.log;
access_log /var/log/nginx/access.log;
}
My "default" Laravel 8 .htaccess:
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews -Indexes
</IfModule>
RewriteEngine On
# Handle Authorization Header
RewriteCond %{HTTP:Authorization} .
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
# Redirect Trailing Slashes If Not A Folder...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} (.+)/$
RewriteRule ^ %1 [L,R=301]
# Send Requests To Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
</IfModule>
My .env:
APP_NAME=SomeSite.com
APP_ENV=production
APP_KEY=base64:sdljkldasjkasjasdjklaslkasd
APP_DEBUG=true
APP_URL=https://www.example.com#Used for CLI function such as artisan...
#ASSET_URL=https://asseturl.test#Only used in asset()
TRUSTPROXIES=*
...
Solution is moving the 301 redirect from Nginx Webserver to Nginx Reverse Proxy (superior level).
This is the following part of the configuration file:
server {
listen 80;
server_name example.com;
return 301 https://www.example.com$request_uri;
}
Edit: I suspect it's related to the part:
proxy_set_header Host $host;
Server: DigitalOcean (Ubuntu)
I am setting a server. I have a Laravel application that is currently setup to be accessed via subdomain, e.g. subdomain.mydomain.com. But I would like to access my application from the main domain, not subdomain.
For instance: instead of subdomain.domain.com, I would like to use domain.com. Below is my 000-default.conf configuration.
<VirtualHost *:80>
ServerName mydomain.com
ServerAlias www.mydomain.com
DocumentRoot "/var/www/html/myapplication/public"
<Directory "/var/www/html/myapplication/public/">
AllowOverride All
</Directory>
RewriteEngine on
RewriteCond %{SERVER_NAME} =www.mydomain.com [OR]
RewriteCond %{SERVER_NAME} =mydomain.com
RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,NE,R=permanent]
</VirtualHost>
Any help is highly appreciated!
You are missing the configuration for the php interpreter. i suggest you copy your old subdomain config and just change ServerName, ServerAlias.
here's an example of what you are missing:
location ~ .php$ {
set $path_info $fastcgi_path_info;
root /var/www/html/myapplication/public;
fastcgi_index app.php;
fastcgi_split_path_info ^(.+\.php)(/.*)$;
try_files $uri $uri/ /app.php$is_args$args /index.php$is_args$args;
include /etc/nginx/fastcgi_params;
fastcgi_pass 127.0.0.1:9000;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
i'm setting up a project in Laravel which makes use of a custom vagrant box based on Ubuntu 14.04 and which runs NGINX and Apache in a typical reverse proxy setup.
Now, the issue is that something along the way is not registering the URL path, so when i enter http://mydomain.test/hello what i ultimately get is just a http://mydomain.test/index.php on the other end and it seems to completely forget about the route param.
I'm using Apache 2.4.7 with Mod PHP and the Apache config is as follows.
<VirtualHost 127.0.0.1:8080>
ServerName application.test
ServerAlias www.application.test
DocumentRoot "/home/vagrant/application/public"
ErrorLog /var/log/apache2/error.log
CustomLog /var/log/apache2/access.log combined
<Directory "/home/vagrant/application/public">
Require all granted
AllowOverride all
</Directory>
</VirtualHost>
As indicated Apache is listening on Port 8080 and NGINX is listening on port 80 with the following config.
server {
listen 80;
server_name application.test www.application.test;
root /home/vagrant/application/public;
index index.php index.html;
location / {
try_files $uri $uri/ /index.php?$is_args$args;
}
location ~ \.php$ {
proxy_pass http://localhost:8080;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
}
location ~ /\. {
deny all;
}
}
I can confirm that all Laravel requirements are being fulfilled, so i have MBString, OpenSSL, etc... I can also confirm that the rewrite module is working as i can add a redirect clause to my .htaccess and it does get executed as expected.
This is my .htaccess file.
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews -Indexes
</IfModule>
RewriteEngine On
# Handle Authorization Header
RewriteCond %{HTTP:Authorization} .
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
# Redirect Trailing Slashes If Not A Folder...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} (.+)/$
RewriteRule ^ %1 [L,R=301]
# Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
</IfModule>
Am i possibly missing something here? I've configured NGINX and PHP-FPM before but it's my first time doing it with Apache, i've gone through multiple similar questions but none of the proposed fixes has yet worked for me and that's why i ask this question.
Thank you in advance for your help
EDIT
Some progress.
If i set the location config for NGINX as follows
location / {
proxy_pass http://localhost:8080;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
}
Then it works, but to the best of my knowledge then this means that even static file requests would end up proxied to Apache.
location ~ \.php$ doesn't seem to work as the laravel pretty url doesn't end with a ".php" suffix i guess. But how would one go about making these two play nice with one another?
The idea i guess is that you have a default location that directs to index.php and when that index.php file is triggered then nginx receives another HTTP request that matches your php location block and will proxy the request to Apache.
The issue is that during this second request, all knowledge of the URI was lost and the URI is now just "index.php" so as far as Apache knows you're always making requests to /index.php.
I still don't know how to fix it though.
So i think i've figured it out, if you have a similar problem, a possible solution is to add the $uri variable to the try_files directive
server {
listen 80;
server_name application.test www.application.test;
root /home/vagrant/application/public;
index index.php index.html;
location / {
try_files $uri $uri/ /index.php$uri;
}
location ~ \.php {
proxy_pass http://localhost:8080;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
}
location ~ /\. {
deny all;
}
}
That way, when nginx calls your index.php it'll call it like http://application.test/index.php/hello and this is the URL that Apache ultimately receives and processes through PHP.
So then, .htaccess comes into play, Laravel does it's thing and everything works, and this still only affects PHP files, your static files should still be served directly by NGINX.
If you find anything wrong with this approach please let me know.
UPDATE
A dumb mistake on my end later down the line lead me to believe something else was wrong, every route seemed to display the contents of my "/" root route.
After trying to use Homestead instead of my custom setup i was surprised to find it affected Homestead as well, so it had to be related to my app itself and not the Server.
Basically it was the Exception handler returning the root view because my requests were missing some data required by my FormRequests, after changing my Exception handler class to handle JSON responses it worked as expected.
If you fall into this kind of trap, try to make sure your app works with Homestead, if it does then there's something wrong with your vagrant setup, if not, then it's your Laravel app that's to blame.
If your Exception Handler just does the typical render, then be wary of the fact it'll attempt to redirect to the previous route, which if you're developing an API with Postman will be "/".
I have apache with ngnix as a reverse proxy.
I have
RewriteRule ^thumb/(.*)x(.*)/r/(.*) thumb.php?w=$1&h=$2&src=$3
RewriteRule ^medias/(.*) files.php?file=$1
How to rewrite theme in nginx ?
Regards
The essential difference between Apache and nginx URIs, is that all nginx URIs begin with a leading /.
You should try:
rewrite ^/thumb/(.*)x(.*)/r/(.*) /thumb.php?w=$1&h=$2&src=$3? last;
rewrite ^/medias/(.*) /files.php?file=$1? last;
See this document for more.
Magento is running just fine on my Nginx + PHP-FPM stack, but I'm getting redirected to the home page every time I delete a product from the cart or add a tag on the product page.
I'm aware that replacing $this->_redirectReferer(Mage::getUrl('*/*')); with $this->_redirect('checkout/cart'); on app/code/core/Mage/Checkout/controllers/CartController.php 'fixes' the cart issue, but what about the tag one? Is there any rule I could add/improve on nginx.conf instead of hacking the Magento core? Strangely, that only happens on Ngix, not on Apache.
Here's an excerpt of my config:
location #handler { ## Magento uses a common front handler
rewrite / /index.php;
}
location ~ .php/ { ## Forward paths like /js/index.php/x.js to relevant handler
rewrite ^(.*.php)/ $1 last;
}
# Pass PHP scripts on to PHP-FPM
location ~* \.php$ {
if (!-e $request_filename) { rewrite / /index.php last; } ## Catch 404s that try_files miss
#expires off; ## Do not cache dynamic content
fastcgi_pass unix:/var/spool/phpfpm.sock; ## php-fpm socket
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
#fastcgi_param MAGE_RUN_CODE default; ## Store code is defined in administration > Configuration > Manage Stores
#fastcgi_param MAGE_RUN_TYPE store;
include fastcgi_params; ## See /etc/nginx/fastcgi_params
}
Looking back as far as Magento CE 1.4.2, a stock Magento CartController.php controller class doesn't have any code that looks like this
$this->_redirectReferer(Mage::getUrl('/'));
I suspect you're dealing with an already hacked system. I'd download a fresh installation and diff your CartController.php file. Based on the information you provided my guess is someone replaced the following line
$this->_redirectReferer(Mage::getUrl('*/*'));
with the line above that's causing your problems.