Nginx Configuration for Wordpress AND Laravel Application - laravel

I'm trying to get my Wordpress blog hosted on blog.domain.com to work on domain.com/blog.
I have been able to do this with proxy_pass successfully but the issue is certain scripts such as the login page for admins (/wp-login.php) does not work - it gives me File not found.. I have isolated the issue to my Laravel applications FastCGI setup in my Nginx configuration. When I comment it out, my blog works but then my Laravel app is inaccessible. I've been trying different configurations but I have little knowledge with Nginx.
server {
listen 443 ssl http2;
listen [::]:443 ssl http2;
server_name mydomain.com;
server_tokens off;
root /home/appfolder/mydomain.com/public;
...
...
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 (DO NOT REMOVE!)
include forge-conf/mydomain.com/server/*;
location / {
try_files $uri $uri/ /index.php?$query_string;
}
# WHEN I COMMENT OUT THIS BLOCK, BLOG WORKS BUT LARAVEL BREAKS
location ~ \.php$ {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php/php7.4-fpm-mydomain.sock;
fastcgi_index index.php;
include fastcgi_params;
}
location /blog {
proxy_redirect off;
proxy_set_header Host $host;
proxy_pass https://blog.mydomain.com;
}
location /blog/wp-content {
proxy_pass https://blog.mydomain.com/wp-content;
}
location /blog/wp-includes {
proxy_pass https://blog.mydomain.com/wp-includes;
}
location /blog/wp-content/uploads {
proxy_pass https://blog.mydomain.com/wp-content/uploads;
}
location /blog/wp-login.php {
proxy_pass https://blog.mydomain.com/wp-login.php;
}
location /blog/wp-admin {
proxy_pass https://blog.mydomain.com/wp-admin;
}
location /bitnami {
proxy_pass https://blog.mydomain.com/bitnami;
}
location /mod_pagespeed_beacon {
proxy_pass https://blog.mydomain.com/mod_pagespeed_beacon;
}
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/mydomain.com-error.log error;
error_page 404 /index.php;
location ~ /\.(?!well-known).* {
deny all;
}
}
...
Hosted on an Ubuntu server managed by Laravel Forge. Any ideas?
Update: As per Richards answer, I have started getting it working a little bit but I need to add rules for all files which doesn't seem efficient.
I'm trying to get all .PHP files related to Wordpress (after /blog/) to be forwarded to the proxy. I have tried:
location ~ /blog/wp-admin/(\d+) {
add_header X-debug-message "$1";
proxy_pass https://blog.mydomain.com/wp-admin/$1;
}
But this doesn't seem to work.

I sorted it out by creating a regular expression to match anything after /wp-admin/ on forward it to the proxy. The configuration I use is:
location ~ (blog\/wp-admin)\/(.*)\.php$ {
proxy_pass https://blog.mydomain.com/wp-admin/$2.php?$query_string;
}
$2 is the second capture group (.*) which is appended to the proxy_pass URL and added any further arguments with ?$query_string.

Related

Nginx throws 404 error for a file route in Laravel

I have a simple Laravel route to provide to our users private files. We present files to users with the help of a middleware. Thus, we check whether the logged in user really has access to this file.
Everything is working as expected we on local environment. But we ran into a problem when we deployed it to the server. It does not see the Laravel route file in the Nginx location and tries to find the file in the root directory.
Laravel route example as below.
Route::get('lesson-attachment/{uuid}/{filename}', LessonAttachmentController::class)->name('lesson.attachment');
As you can see, we expect to receive a uuid and filename as url parameters. Actually the filaname parameter is required because we returns inline response on the exact url sometimes and we want to show the filename on the browser tab. Anyway, finally the codes here work fine on local machine.
But when we come to the nginx server, things get confused and when we look at the logs, we get the following error.
/var/www/vhosts/example.com/httpdocs/current/public/lesson-attachment/61cd7c8b-a7a1-49f8-86b8-d82825c9f55a/sample-file.pdf" failed (2: No such file or directory)
Here is the nginx conf:
add_header X-Frame-Options "SAMEORIGIN";
add_header X-Content-Type-Options "nosniff";
index index.php;
charset utf-8;
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; }
error_page 404 /index.php;
The question is how can I run that url path on production server?
https://example.com/lesson-attachment/61cd7c8b-a7a1-49f8-86b8-d82825c9f55a/sample-file.pdf
For more information my development environment:
Macbook Pro
Laravel Valet
For more information our production environment:
Ubuntu 20
Nginx
Plesk Panel (core nginx configurations are provided from plesk)
Using php deployer tool for deployment to production.
UPDATE - The base server blocks of nginx configuration. Please note, it provides by Plesk.
#ATTENTION!
#
#DO NOT MODIFY THIS FILE BECAUSE IT WAS GENERATED AUTOMATICALLY,
#SO ALL YOUR CHANGES WILL BE LOST THE NEXT TIME THE FILE IS GENERATED.
map $sent_http_content_type $expires {
default off;
text/html epoch;
text/css max;
application/javascript max;
~image/ max;
~font/ max;
}
server {
listen ***** ssl http2;
expires $expires;
server_name example.com;
client_max_body_size 134217728;
# mailconfig
location ~* ^/autodiscover/autodiscover\.xml$ {
try_files $uri #mad;
}
location ~* ^(/\.well-known/autoconfig)?/mail/config\-v1\.1\.xml$ {
try_files $uri #mad;
}
location ~* ^/email\.mobileconfig$ {
try_files $uri #mad;
}
location #mad {
rewrite ^(.*)$ /mailconfig/ break;
proxy_pass http://127.0.0.1:8880;
proxy_set_header X-Host $host;
proxy_set_header X-Request-URI $request_uri;
}
# mailconfig
root "/var/www/vhosts/example.com/httpdocs/current/public";
access_log "/var/www/vhosts/system/example.com/logs/proxy_access_ssl_log";
error_log "/var/www/vhosts/system/example.com/logs/proxy_error_log";
#extension letsencrypt begin
location ^~ /.well-known/acme-challenge/ {
root /var/www/vhosts/default/htdocs;
types { }
default_type text/plain;
satisfy any;
auth_basic off;
allow all;
location ~ ^/\.well-known/acme-challenge.*/\. {
deny all;
}
}
#extension letsencrypt end
#extension sslit begin
#extension sslit end
#extension sectigo begin
location ^~ /.well-known/pki-validation/fileauth.txt {
try_files $uri $uri/ =404;
}
location ^~ /.well-known/pki-validation/ {
root /var/www/vhosts/default/htdocs;
types { }
default_type text/plain;
satisfy any;
auth_basic off;
allow all;
location ~ ^/\.well-known/pki-validation.*/\. {
deny all;
}
}
#extension sectigo end
error_page 400 "/error_docs/bad_request.html";
error_page 401 "/error_docs/unauthorized.html";
error_page 403 "/error_docs/forbidden.html";
error_page 404 "/error_docs/not_found.html";
error_page 500 "/error_docs/internal_server_error.html";
error_page 405 "/error_docs/method_not_allowed.html";
error_page 406 "/error_docs/not_acceptable.html";
error_page 407 "/error_docs/proxy_authentication_required.html";
error_page 412 "/error_docs/precondition_failed.html";
error_page 414 "/error_docs/request_uri_too_long.html";
error_page 415 "/error_docs/unsupported_media_type.html";
error_page 501 "/error_docs/not_implemented.html";
error_page 502 "/error_docs/bad_gateway.html";
error_page 503 "/error_docs/maintenance.html";
location ^~ /error_docs {
root "/var/www/vhosts/example.com";
}
location ~ /\.ht {
deny all;
}
location ~ ^/(plesk-stat|awstats-icon|webstat|webstat-ssl|ftpstat|anon_ftpstat) {
auth_basic_user_file "/var/www/vhosts/system/example.com/pd/d..httpdocs#plesk-stat";
autoindex on;
location ~ ^/plesk-stat(.*) {
alias /var/www/vhosts/system/example.com/statistics/$1;
}
location ~ ^/awstats-icon(.*) {
alias /usr/share/awstats/icon/$1;
}
location ~ ^/(.*) {
alias /var/www/vhosts/system/example.com/statistics/$1;
}
}
location ~ ^/~(.+?)(/.*?\.php)(/.*)?$ {
alias /var/www/vhosts/example.com/web_users/$1/$2;
fastcgi_split_path_info ^((?U).+\.php)(/?.+)$;
try_files $uri $fastcgi_script_name =404;
fastcgi_param PATH_INFO $fastcgi_path_info;
fastcgi_pass "unix:/var/www/vhosts/system/example.com/php-fpm.sock";
include /etc/nginx/fastcgi.conf;
}
location ~ \.php(/.*)?$ {
fastcgi_split_path_info ^((?U).+\.php)(/?.+)$;
try_files $uri $fastcgi_script_name =404;
fastcgi_param PATH_INFO $fastcgi_path_info;
fastcgi_pass "unix:/var/www/vhosts/system/example.com/php-fpm.sock";
include /etc/nginx/fastcgi.conf;
}
index "index.html" "index.cgi" "index.pl" "index.php" "index.xhtml" "index.htm" "index.shtml";
disable_symlinks if_not_owner "from=/var/www/vhosts/example.com";
add_header X-Powered-By PleskLin;
include /etc/nginx/conf.d/expires.global;
include "/var/www/vhosts/system/example.com/conf/vhost_nginx.conf";
}

Nginx 404 Not Found Error. How can I fix it?

I'm using Laravel 8 for my project.
Php: 7.4
Using AWS Nginx Server
My Application's Homepage is fine. The homepage is working well. But, when I'm clicking a link then I'm getting a 404 Not Found error from Nginx.
Here is my server config in nginx.conf
server {
listen 80;
listen [::]:80;
server_name something.com;
root /srv/something.com;
add_header X-Frame-Options "SAMEORIGIN";
add_header X-Content-Type-Options "nosniff";
index index.php;
charset utf-8;
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; }
error_page 404 /index.php;
location ~ \.php$ {
fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
include fastcgi_params;
}
location ~ /\.(?!well-known).* {
deny all;
}
}
What to do? What to change?
How to fix this error?

Why Nuxtjs Axios Proxy is not working on server?

I'm struggling for few days to find a solution. It seems that my nuxt/axios proxy configuration are not taken into account when my site is on production. Locally everything is working fine but once the site is on the server my ajax calls hit mysite.com/api/ect... instead of being proxy to mysite.com/api/v1/ect. I tried to play with axios.baseURL and various configuration but nothing seems to work.
axios: {
proxy: true,
credentials: true,
},
proxy: {
'/api/': { target: 'mysite.com/api/v1', pathRewrite: {'^/api/': ''} },
},
Maybe the issue comes from my Nginx configuration ? I use a reverse proxy to serve a nuxt app on mysite.com and a laravel api on mysite.com/api. Can this be the problem ?
# FORGE CONFIG (DO NOT REMOVE!)
include forge-conf/mysite.com/before/*;
server {
listen 443 ssl http2;
listen [::]:443 ssl http2;
server_name mysite.com;
server_tokens off;
root /home/forge/api/public;
# FORGE SSL (DO NOT REMOVE!)
ssl_certificate /etc/nginx/ssl/....
ssl_certificate_key /etc/nginx/ssl/....
ssl_protocols TLSv1.2 TLSv1.3;
ssl_ciphers .....
ssl_prefer_server_ciphers off;
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 (DO NOT REMOVE!)
include forge-conf/mysite.com/server/*;
location /api {
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/mysite.com-error.log error;
error_page 404 /index.php;
location ~ \.php$ {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
fastcgi_index index.php;
include fastcgi_params;
}
location / {
proxy_pass http://127.0.0.1:3000;
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 ~ /\.(?!well-known).* {
deny all;
}
}
# FORGE CONFIG (DO NOT REMOVE!)
include forge-conf/mysite.com/after/*;
Thanks for your help and recommendation,
Don't use API_URL if you use proxy mode. Use prefix instead of.
Turn on debug for check the proxyRequest:
proxy property in nuxt.config.js:
proxy: {
// target, others options
logLevel: ‘debug’,
onProxyReq(proxyReq, req, res) {
// console.log here
}
}

Nuxt.js front end and laravel api on same nginx server Digital ocean

I have a nuxt application SPA for the front end and a laravel API. Nuxt calls on API for request. I am trying to deploy this in one digital ocean droplet but I am having problems with it. My laravel application seems to be working but I cant get nuxt to show here is my set up
Ubuntu 20
nginx 1.18
php 7.4
laravel nginx server block
server {
listen 80;
server_name DROPLET_IP;
root /var/www/laravel-api/public;
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 = /favicon.ico { access_log off; log_not_found off; }
location = /robots.txt { access_log off; log_not_found off; }
error_page 404 /index.php;
location ~ \.php$ {
fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
include fastcgi_params;
}
location ~ /\.(?!well-known).* {
deny all;
}
}
here is my nuxt server block:
server {
listen 3000;
server_name DROPLET_IP;
keepalive_timeout 60;
index index.html;
location / {
root /var/www/nuxt-front/dist;
}
}
both of these are in their own sites-available and symlink to sites enable.
for some reason when I access http://DROPLET_IP:3000. it just hangs.
Is there a special way I should be doing this to run as expected?
Use root
server {
listen 80;
server_name example.com;
keepalive_timeout 60;
root /var/www/nuxt-front/dist;
}
or
use proxy pass
server {
listen 80;
server_name example.com;
location / {
proxy_pass http://127.0.0.1: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;
}
}

SSL laravel forge in default site

I have read this tutorial to set up SSL for my laravel site. I did not read everything (oops), and did not delete the "default" site. I have generated CSR from default site (but with example.com for domain). Now I have my certificate and i have installed it through laravel forge, but it does not work:
cannot connect to https://example.com (browser / curl)
http://example.com does not redirect to https
Nothing in /var/log/nginx/default-error.log
I restarted nginx
Here is my /etc/nginx/sites-available/default file content :
server {
listen 80;
#server_name default;
server_name example.com www.example.com;
#return 301 default$request_uri;
return 301 https://example.com$request_uri;
}
server {
listen 443 ssl;
#server_name default;
server_name example.com www.example.com;
root /home/forge/default/public;
# FORGE SSL (DO NOT REMOVE!)
ssl_certificate /etc/nginx/ssl/default/XXXXX/server.crt;
ssl_certificate_key /etc/nginx/ssl/default/XXXXX/server.key;
index index.html index.htm index.php;
charset utf-8;
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/default-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;
}
}
What can I do to make it work ?
Ok, it was a certificate problem. I tried nginx -t and there was an error, now I restarted all the process and everything is alright now.

Resources