Use Wordpress Inside Laravel Nginx - wordpress

I'm trying to have Wordpress installed in other folder than "/public" on Laravel project.
The laravel path:
/home/forge/domain.com/
Wordpress path:
/home/forge/blog.domain.com/
But I want to use this url and not as subdomain:
domain.com/blog

You can use a location section in your nginx configuration file:
location ^blog/ {
root /home/forge/blog.example.com;
//your regular nginx configuration
}
You may also use a reverse proxy. This means you have to put the blog on a different URL (like blog.domain.com) and then point blog/ to that URL. For example:
location ^blog/ {
sub_filter_types *;
sub_filter_once off;
sub_filter 'blog.example.com' 'www.example.com/blog';
proxy_pass http://blog.example.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 Accept-Encoding "";
proxy_redirect http://blog.example.com http://www.example.com;
}
The subfilter rewrites all URLs in your html output. The proxy_redirect makes sure cookies and headers are corrected as well.

Related

nginx - how to have proxy pass render a subdomain on the page?

We have a PHP server with Nginx running. On a separate server hosted by Vercel we have a Next.js app running. We've deployed the Vercel app to a subdomain of our main domain: https://vercel.employbl.com/ Our domain is hosted by Cloudflare and we linked it to Vercel by adding a CNAME record like so:
What I'm trying to do is have our main jobs page instead Render the jobs page of the Vercel app. So user enters https://www.employbl.com/jobs but the Next.js app with the matching path https://vercel.employbl.com/jobs renders instead while keeping "employbl.com/jobs" as the URL.
This is the Nginx config we have currently using proxy_pass. Unfortunately I'm getting a 404 error from Vercel when I navigate to "employbl.com/jobs" using this Nginx config:
location / {
try_files $uri $uri/ /index.php?$query_string;
}
location /jobs {
proxy_set_header X-Forwarded-For $remote_addr;
proxy_set_header Host $http_host;
proxy_pass https://vercel.employbl.com/jobs;
}
This renders the page from Vercel: 404 - Code: DEPLOYMENT_NOT_FOUND but the site is working at the URL provided in the nginx config.
How can I configure Nginx so that when the user navigates to "employbl.com/jobs" the https://vercel.employbl.com/jobs page renders?
Try it like this, with trailing slash after the location directive (/jobs/). Then take /jobs off the proxy URL. I think your location path /jobs is being handed to the proxy_pass and ending up doubling the /jobs/ part somehow. Because with no trailing slash it passes that forward. So try:
location /jobs/ {
proxy_set_header X-Forwarded-For $remote_addr;
proxy_set_header Host $http_host;
proxy_pass https://vercel.employbl.com; #removed jobs
}
Edit: On my server I can only get it to work this way. Both location directive and proxy URL end in a slash. From what I've read, this should make it drop the location directive path, instead of passing it on.
location /jobs/ {
proxy_set_header X-Forwarded-For $remote_addr;
proxy_set_header Host $http_host;
proxy_pass https://vercel.employbl.com/jobs/;
}
We ended up getting it working like this. Needed a separate entry to point to files for the _next directory that holds our assets. I think the primary fix was having resolver 8.8.8.8;
location / {
try_files $uri $uri/ /index.php?$query_string;
}
location /jobs {
resolver 8.8.8.8;
proxy_set_header X-Real-IP $remote_addr;
proxy_pass https://vercel.employbl.com/jobs;
}
location ~ ^/_next/(.*)$ {
resolver 8.8.8.8;
proxy_set_header X-Real-IP $remote_addr;
proxy_pass https://vercel.employbl.com/_next/$1;
}

How do I rewrite request_uri variable in a location match

Java backend give me several interfaces like:
/admin/login
/client/query
/agent/update
I am a frontend engineer and I have to use these interfaces with ajax. Because we will deploy our projects on the same server, so I need to use a nginx proxy to direct those ajax request to the java service. But as you can see the interfaces start with different paths, so I prefixed them with "/api" in my code, and also configure nginx as below
axios.post('/api/admin/login'),
axios.post('/api/client/query'),
axios.post('/api/agent/update')
location /api {
proxy_pass http://127.0.0.1:8080$request_uri;
proxy_set_header Host 127.0.0.1;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
here my problem come:
Actually the java service get the request path: /api/admin/login, not /admin/login, so they can not handle that request. Can I rewrite my request_uri in nginx so that java get requests without /api prefix?
You don't need any rewrites for this. Use following location block:
location /api/ {
proxy_pass http://127.0.0.1:8080/;
proxy_set_header Host 127.0.0.1;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
See more info about slashes in proxy_pass directive here.
Copy pasted and adapted from a different question but should do the trick.
location /api {
rewrite ^/api(/.*)$ $1 last;
}

NGINX: Proxy to blog on wordpress

I have a wordpress based site with blog in /blog location. I wanted to re-use this blog from an other site, but I am redirected to www.example.com. My nginx config of the location is:
location ~ ^/blog {
resolver 8.8.8.8;
proxy_pass https://www.example.com$request_uri;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $remote_addr;
}
How should this config look like?
To make WordPress respond correctly to two different hostnames, you will need to make it hostname agnostic.
Change the WordPress configuration so that the hostname is not part of the HOME and SITEURL parameter values. For example, use a value /blog, instead of https://www.example.com/blog.
See this document for more.
Regarding the location block. It can be simplified as follow:
location ^~ /blog {
resolver 8.8.8.8;
proxy_pass https://www.example.com;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $remote_addr;
}

ASP.Net Core with nginx set host header not working

I'm trying to host my ASP.Net core app using Nginx.
My nginx.conf like this
server {
listen 80;
server_name mydomain.com;
location / {
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;
proxy_pass http://localhost:5000;
}
}
I added this code to Configure method to use forwarded headers
app.UseForwardedHeaders(new ForwardedHeadersOptions {
ForwardedHeaders = ForwardedHeaders.XForwardedFor | ForwardedHeaders.XForwardedProto,
ForwardLimit = null,
RequireHeaderSymmetry = false
});
I navigate to mydomain.com from browser and it works until I click the Loggin with google button. It redirect to http://localhost:5000/signin-google instead of http://mydomain.com/signin-google.
How do I get it redirect to http://mydomain.com/signin-google ?
I believe proxy_redirect directive could help.
According to:
NGinX Documentation for proxy_redirect
proxy_redirect: "Sets the text that should be changed in the “Location” and “Refresh” header fields of a proxied server response."
So:
proxy_redirect http://localhost:5000/ http://example.com/
May be the solution to your problem.
You may even be able to use a variable to make support easier?
proxy_redirect http://localhost:5000/ http://$host:$server_port
Further reading:
Host ASP.NET Core on Linux with Nginx

Configuring Spark Web-UI with nginx

I need configure spark web-ui with nginx .
My configuration
location /app/spark/master {
proxy_pass http://192.168.230.45: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;
}
but when i try to access the url, the images and css are not loaded(404 not found).
Now assume the images and css files in static folder of my remote machine(192.168.230.45) but the url points to static folder of my nginx installed machine.
http://localhost/static/img1.png
instead
http://192.168.230.45/static/img1.png
I've lost a couple of days being looking for solution. Finally I've found out how to resolve this problem.
Here is my resolution of our issue:
location /app/spark/master/ {
proxy_pass http://192.168.230.45:8080/;
proxy_set_header Accept-Encoding "";
sub_filter "/static/" "/app/spark/master/static/";
sub_filter_once off;
}
In addition You'll face there similar problem with "history"-like links.
Solution will be the same. Just add the line:
sub_filter "/history/" "/app/spark/master/history/";
And voila!
I also had the same problem, you should use it as root:
app/spark/master => /
location / {
proxy_pass http://192.168.230.45:8080;
auth_basic "Restricted Content";
auth_basic_user_file /etc/nginx/pwds;
}
or you will need to set a pattern correction;
rewrite ^/(.*) https://$server_name/$1 permanent;

Resources