Nginx location with optional sublocation - wordpress

I have a WordPress site with php-fpm and nginx configured. Works well.
Now I created an Angular Universal app which I want to display on the certain page. To be more specific:
I want WordPress to work here: mysite.com/myapp
I want Angular to work here: mysite.com/myapp/any/path/
I tried so many different configurations but nothing works as I want it to. For instance:
location / {
try_files $uri $uri/ /index.php?$args;
}
location ~ ^/myapp/(.+)$ {
proxy_pass http://127.0.0.1:4000/$1$is_args$args;
proxy_redirect off;
proxy_read_timeout 60s;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_set_header X-NginX-Proxy true;
root /var/www/mysite/myapp/dist/;
}
location ~ \.php$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php-fpm/tst04.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
So as you can see I'm trying to redirect all requests starting with /myapp/{something} to the http://127.0.0.1:4000 but it does not work. By "does not work" I mean that all requests are redirected to the localhost, even /myapp/- which I don't understand because /myapp/ does not match the regex /myapp/(.+) (note the + sign).
I looked into the nginx error log and found that when I enter mysite.com/myapp/ it tries to redirect it to the upstream: "http://127.0.0.1:4000/index.php" which is not desired to me. When I enter mysite.com/myapp/anypath/ it redirects it to the upstream: "http://127.0.0.1:4000/anypath/" which is correct.
Question: why it doesn't work?

Dang! I guess I had to describe the problem somewhere to find the solution ;)
So here's what was happening:
I make a request e.x. mysite.com/myapp
Nginx matches it to the /location
It runs try_files to the end and fires /index.php?$args which resolves to /myapp/index.php
Nginx keeps resolving new location and it matches ^/myapp/(.+)$ so it's being redirected to the http://127.0.0.1:4000/index.php
Tadam! We have the explanation. Now how did I solve it? Well, the solution is pretty simple. I don't want my PHP files to be mathed against ^/myapp/(.+)$ regex so I modified it like this:
"^/myapp/([^ php]+)$" (note the quotation marks)
and everything started to work as expected.
Posting this in case future readers will encounter similar problem.

Related

Nginx configuration for Symfony on a sub uri

I am trying to host two applications under the same domain. One nodejs (nextjs) project and a Symfony application. I was planning to use "/be" sub url for the Symfony application. I was able to setup nginx to route to the respective applications. However I am getting 404 because I cannot replace the "/be" prefix from the request uri while trying to pass the request to the PHP-FPM. I tried replacing the REQUEST_URI fastcgi parameter but same problem persists. Here's my location configurations:
location ~ ^/be/.*$ { # for static files
root /home/ubuntu/be-prereg/public;
# remove /be/ from request url
rewrite ^/be/(.*)$ /$1 break;
# try to serve file directly, fallback to index.php
try_files $uri /index.php$is_args$args;
}
location ~ ^/index\.php(/|$) {
root /home/ubuntu/be-prereg/public;
fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
fastcgi_split_path_info ^(.+\.php)(/.*)$;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
fastcgi_param DOCUMENT_ROOT $realpath_root;
fastcgi_param REQUEST_URI '/api/test';
internal;
}
#nodejs
location / {
proxy_pass http://localhost:3000;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
}

Ngnix/Wordpress seamless redirect subfolder to subdirectory with proxy-pass

I have two servers for one domain
server A hosting mywebsite.com and
server B hosting blog.mywebsite.com (Blog WordPress).
My goal is to have a seamless url for my blog with: http://mywebsite.com/test23 with the content of server B.
My first redirection is working (location /test23). I can get my home page and get other pages but I am not able to connect to my WP admin. I am getting a 404 on mywebsite.com/test23/wp-login.php.
Server A nginx conf file
index index.php index.html index.htm;
server_name mywebsite.com;
location / {
try_files $uri $uri/ /index.php?$args;
}
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/run/php/php7.0-fpm.sock;
fastcgi_read_timeout 1000;
}
location /test23 {
rewrite ^/test23(.*) /$1 break;
proxy_pass http://blog.mywebsite.com/;
}
location /test23/wp-admin {
rewrite ^/test23/wp-admin(.*) /$1 break;
proxy_pass http://blog.mywebsite.com/wp-admin;
}
I guess I need to manage exclusion of the wp-admin folder? I m kind of lost :D
Since you proxy the "/test23" path and you use a rewrite rule to bind anything using that adress you don' need the second location block.
Try using this:
location /test23 {
rewrite ^/test23(/.*)$ /$1 break;
proxy_pass http://blog.mywebsite.com/;
}
If you haven't set proxy rules , this block may also look like:
location /test23 {
rewrite ^/test23(/.*)$ /$1 break;
proxy_pass http://blog.mywebsite.com/;
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 https;
proxy_redirect off;
}
You should use a backslash after"test23" in the rewrite. In the first case it works because there is nothing after /test23 in the request.
In the second request it gets mixed up because the position of the location blocks matters. So it gets rewritten by the first rule, which leads to an error without the backslash.

nginx => Serve two websites from one server block with sub-domain

I'd like to serve two applications from the same server through nginx. I'd like these applications to be available through a single domain name with sub-uris.
e.g.
www.example.com => should serve normal example site.
www.example.com/blog => blog.example.com (wordpress) site which is at different directory
Here, anything user requests from /blog/... should be served from blog site, but url should be www.example.com/blog/...
I have tried this.
server {
root /web/servers/example/public;
...
...
location /blog {
proxy_pass http://blog.example.com;
proxy_redirect off;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
Other try was,
location /blog {
alias /web/servers/blog/public;
index index.php index.html index.htm;
try_files $uri $uri/ /index.php?$args;
# pass the PHP scripts to FastCGI server
location ~ \.php$ {
#fastcgi_split_path_info ^(.+\.php)(/.+)$;
#allow 127.0.0.1;
fastcgi_pass 127.0.0.1:9000;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
#fastcgi_index index.php;
}
}
None of the options are working for /blog. Main site is working fine.
Could anybody please help me to solve the issue?

cURL works but browser (through NGINX) won't load

I am using NGINX and Unicorn for my Rails app. I've hit a brick wall and am not sure what is happening.
I have set NGINX logging to full debug. When I attempt to browse to my site home page, I get problem loading page and nothing in the NGINX error log. When I use cURL to get to the same page, it works perfectly and the log is full of helpful information. I'm hoping this is an obvious error in my NGINX configuration file:
upstream unicorn-soup {
server unix:/home/soup/app/tmp/sockets/unicorn.sock;
}
server {
listen 80 default;
listen [::]:80 default;
root /home/soup/app/current/public;
server_name soup.quote2bill.com;
try_files $uri/index.html $uri.html $uri #unicorn;
location #unicorn {
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_set_header X-Forwarded_Proto $scheme;
proxy_redirect off;
proxy_pass http://unicorn-soup;
proxy_read_timeout 300s;
proxy_send_timeout 300s;
}
}
Note I even tried setting this to the default site and browsing to the IP address but it didn't help.
Thanks for your time and suggestions on how to troubleshoot and/or fix.
Fixed! I had dropped the location directive for the try line.
Corrected:
location / {
try_files $uri/index.html $uri.html $uri #unicorn;
}

nginx-apache-wordpress: setup apache/wordpress under subdirectory and behind nginx

I had developed a Rails app and was happy with it, but I was asked to setup Wordpress under subdirectory /wp. After some unhappy hours of trying to make nginx-to-apache proxying work, I gave up copying code from shitty guides and wrote some very short and clear config:
location #wp {
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_redirect off;
proxy_pass http://127.0.0.1:8080;
}
location ~ ^/wp(/|$) {
root /home/apache/www;
rewrite ^/wp/?$ / break;
rewrite ^/wp/(.*) /$1 break;
try_files $uri #wp;
}
It solved most of problems I've had experienced, but it won't work for an obvious reason:
Wordpress generates <real_ip>:8080/<url> links and that is not just ugly (I can live with it), but links don't work since Apache listens on localhost only.
How can I tell Apache or Wordpress (or what header should I proxy with Nginx) to make links look like <real_ip>/wp/<url>? Or is my setup faulty by design? I would appreciate any solution or hint, thanks!
Below is my configuration for nginx, which runs a Rails app at the main root, and a WordPress blog under a subdirectory. Perhaps try this?
server {
listen <ip-address>:80;
server_name domain.com;
rewrite ^(.*)$ $scheme://www.domain.com$request_uri? permanent;
break;
}
server {
listen <ip-address>:80;
server_name www.domain.com;
root /www/domain.com/public_html/current/public;
access_log /www/domain.com/logs/access.log;
error_log /www/domain.com/logs/error.log;
location ^~ /blog {
root /www/domain.com/blog/public_html;
index index.php index.html index.htm;
try_files $uri $uri/ /blog/index.php?$args;
location ~ \.php$ {
include /etc/nginx/fastcgi_params;
if ($uri !~ "^/images/") {
fastcgi_pass unix:/var/run/php-fastcgi/php-fastcgi.socket;
}
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /www/domain.com/blog/public_html$fastcgi_script_name;
}
}

Resources