Nginx multiple locations laravel project - laravel

I am having issues trying to setup a second set of locations for my Nginx Setup. I currently have 1 working route, that uses reverse proxy to a NodeJS Express server.
I am tryin to setup a second location to serve a laravel project, here is my Nginx config file, I know there are some errors, but after googling I coulnd't find an answer on my own.
Thanks
server {
listen 443 http2 ssl;
listen [::]:443 http2 ssl;
server_name some.server.com
ssl on;
ssl_certificate /etc/letsencrypt/live/some.server.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/some.server.com/privkey.pem;
# This doesnt work, its a laravel project
# Theres something wrong with my try_files
location /project2 {
root /home/ubuntu/project2/public/;
try_files $uri $uri/ /index.php?$query_string;
}
# This works, I am reverse proxying to NodeJS Application
location / {
proxy_pass http://localhost:3001;
proxy_set_header Host $http_host;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
}
}

You need to add something to the /project2 that tells nginx how to handle php files. a block that will know what to do with
I grabbed the following from here. I updated your location regex, though I haven't tested, so you may need to fix it (everything about nginx is trial and error until it works):
location ~* /project2/(.*\.php)$ {
root /home/ubuntu/project2/public/;
try_files $1 $1/ $1/index.php?$query_string;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
You are serving from /home/ubuntu/project2/public/, but your php urls end with project2, so you need to do a bit of regex magic to extract the proper url.
Of course, if you can make things simpler with your directory structure, you can use a simpler regex.

Related

Nginx Server Configuration: Hostname not resolving on Subdomain

thank you in advance for your support.
I set up an Ubuntu Server with Nginx as a Digitalocean Droplet and am using the server provisioning tool Laravel Forge, which works fine. I successfully installed the PHP Framework Laravel and deployed the code on the server. I ssh into the server and checked the files in the supposed directory. The code is successfully deployed.
Next I own a domain, and created an A record for the following subdomain: app.mywebsite.de, that points to that server. I followed the digitalocean instructions and I waited the required time. Using a DNS Lookup tool, I confirmed that the subdomain actually points to the server.
Screenshot of DNS Lookup
Yet, when I use the subdomain in my browser, the browser doesn't connect to the server. I get the following error message in the browser:
Screenshot of Browser when connecting to server
It seems like the subdomain is correctly pointed to the server, but the server isn't rightly configured. I tried to check the nginx configuration and under sites-avaialble I have the following configuration for the subdomain:
# FORGE CONFIG (DO NOT REMOVE!)
include forge-conf/app.mywebsite.de/before/*;
server {
listen 80;
listen [::]:80;
server_name app.mywebsite.de;
server_tokens off;
root /home/forge/app.mywebsite.de/public;
# FORGE SSL (DO NOT REMOVE!)
# ssl_certificate;
# ssl_certificate_key;
ssl_protocols TLSv1.2 TLSv1.3;
ssl_ciphers TLS13-AES-256-GCM-SHA384:TLS13-CHACHA20-POLY1305-SHA256:TLS_AES_256_GCM_SHA384:TLS-AES-256-GCM-SHA384:TLS_CHACHA20_POLY1305_SHA256:TLS-CHACHA20-POLY1305-SHA256:ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-ECDSA-CHACHA20-POLY1305:ECDHE-ECDSA-AES256-SHA384:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-RSA-CHACHA20-POLY1305:ECDHE-RSA-AES256-SHA384:ECDHE-ECDSA-AES256-SHA:ECDHE-RSA-AES256-SHA;
ssl_prefer_server_ciphers on;
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/app.mywebsite.de/server/*;
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/app.mywebsite.de-error.log error;
error_page 404 /index.php;
location ~ \.php$ {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php/php8.0-fpm.sock;
fastcgi_index index.php;
include fastcgi_params;
}
location ~ /\.(?!well-known).* {
deny all;
}
}
# FORGE CONFIG (DO NOT REMOVE!)
include forge-conf/app.mywebsite.de/after/*;
In forge-conf/app.website.de/before/*; is only one file redirect.conf with the following code:
server {
listen 80;
listen [::]:80;
server_name www.app.website.de;
if ($http_x_forwarded_proto = 'https') {
return 301 https://app.website.de$request_uri;
}
return 301 $scheme://app.website.de$request_uri;
}
There are no other sites on the server. So there is only the 000-catch-all file in the sites available directory of the nginx configuration folder.
Unfortunately I reached my limit of understanding here and I would love if somebody could point me into the right direction to find out, which part of nginx is not configured corectly.
P.S.
Some additional info:
Yes I restarted Nginx and the whole server multiple times.
Turns out, everything was configured correctly. I didn't change anything, except that I added some additional sites on the nginx server. Forge probably updated the server blocks, which resolved the problem.

nginx configuration for a nodejs app and laravel app together

So I have a web app project on my server with 2 directories:
1) frontend - a nodejs app (next.js) that I start with pm2.
2) api - the laravel backend api
I also the have nginx installed and tried this as my server settings:
server {
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;
}
listen 80;
listen [::]:80;
root /project/api/public;
index index.php index.html index.htm;
location / {
try_files $uri/ /index.php?$query_string;
}
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
}
Now when i go to my web address the frontend part works so when i go to example.com I see my web app (so the config for the proxy to port 3000 works).
Howvever the backend/api does not work, i think because it does not redirect to the api when i perform a backend task. like
http://example.com:3000/api/auth/login/ should go to my laravel app.
"/api/..." is the backend endpoints.
I think I need to somehow define a location /api {} to go to the laravel app.
Any help to get ths working would be appreciated.
You have 2 "location /" definitions... One is clobbering the other.
Try using the following for your second definition:
location /api {
try_files $uri/ /index.php?$query_string;
}

Endless loading/spinner on the woocommerce checkout page

Since some recent updates , the checkout page on my site is not working. Even after entering name/address, the area under “Your order” is greyed out and spinning.
Things I’ve tried that didn’t work:
Disabled all plugins (except woocommerce)
Changed theme to Default theme.
Checked Woocommerce>System Status.
Increased memory limit via wp-config Disabled PayPal (the only payment gateway)
Removed the woocommerce files via admin and
uploaded a fresh version via FTP
Checked items on this page: http://docs.woothemes.com/document/endless-
loadingspinner-on-the-checkout-page/
I'm runngin NGINX 1.6.3 server and seems to be no issue with server as other woocommerce sites on same server works fine.
I’ve looked at the Chrome console and found this:
https://example.com/?wc-ajax=get_refreshed_fragments Failed to load resource: the server responded with a status of 405 (Not Allowed)
I checked using full URL like https://example.com/index.php?wc-ajax=get_refreshed_fragments and it shows some ajax code but using https://example.com/?wc-ajax=get_refreshed_fragments shows homepage.
Disabling Ajax completely on checkout page works but it does not update price for checkout addons like gift wrap.
This is config for my site:
server {
listen 80;
server_name example.com www.example.com;
return 301 https://$server_name$request_uri;
}
server {
listen 443 ssl spdy;
ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem;
server_name example.com www.example.com;
root /home/user/example.com/public/;
index index.php;
access_log /home/user/example.com/logs/access.log;
error_log /home/user/example.com/logs/error.log;
location / {
try_files $uri $uri/ /index.php?$args;
}
location ~ /.well-known {
allow all;
}
location ~ \.php$ {
try_files $uri =404;
fastcgi_pass 127.0.0.1:9000;
# With php5-fpm: fastcgi_pass
#unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}

Nginx and PhpMyAdmin - Redirect to images

I'm running Ubuntu Server 14.04. I installed Tomcat 7 as a server and Nginx as a reverse proxy. Then I also installed PhpMyAdmin and got that working. The only problem I now have is that none of the images for PhpMyAdmin is showing. I've done a lot of searching on this issue and have found several ways to fix it but none of them work for particular instance. I set up my Nginx configuration in such a way that any request gets forwarded to my Tomcat server except when it ends in .php, in which case it forwards (correctly) to PhpMyAdmin. Here's (the important parts) of my nginx configuration (etc/nginx/sites-enabled/default):
server {
listen 80 default_server;
server_name 123.123.123.123; # fake Ip address
root /usr/share/nginx/html;
location / {
proxy_set_header X-Forwarded-Host $host;
proxy_set_header X-Forwarded-Server $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass http://tomcat_server;
}
location ~ \.php$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
I have a very basic knowledge of the above, so excuse me for not knowing how the location closures really work or what their contents do.
What I think I need to do is rewrite those to location closures in such a way that requests containing /phpmyadmin/ + anything here + .png or .jpg, etc gets forwarded to the right directory. Please let me know if this is not the best way of doing it though. Otherwise please let me know how to rewrite my location closures to do what I want it to do. It seems that PhpMyAdmin's images are stored at /usr/share/nginx/html/phpmyadmin/themes/ if that helps. Let me know if I need to supply any additional information.

NGINX CORS ISSUE

Is there a way around to avoid CORS issue using nginx. My one application is running no netty-server which comes with play framework using joc.lan as domain name and other application is on php web server which i had integrated in my application which uses iframe to load and it uses chat.joc.lan as domain name which is a subdomain of joc.lan.
So,when anyone of my application tries to access any data for other application,the error i get on console is
Uncaught SecurityError: Blocked a frame with origin
"http://chat.joc.lan" from accessing a frame with origin
"http://joc.lan". Protocols, domains, and ports must match.
I had resolved this error by setting document.domain on both application as the main domain name which is joc.lan.
And for ajax requests i am using JSONP.
I had read somewhere it's not supported on firefox and IE.
The first once is for my main application joc.lan
server {
listen 80;
server_name joc.lan;
location / {
proxy_pass http://localhost:9000;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
# WebSocket support (nginx 1.4)
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
}
}
the second one i am interating inside joc.lan using iframe.
server {
listen 80;
server_name chat.joc.lan;
root /opt/apps/flyhi/chat;
index index.php;
# caching for images and disable access log for images
location ~* \.(jpg|jpeg|gif|png|css|js|ico|xml|ttf|eot)$ {
access_log off;
expires 360d;
}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9007
location ~ \.php {
fastcgi_pass 127.0.0.1:9011;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
access_log off;
}
location / {
try_files $uri $uri/ /index.php?r=$request_uri;
}
I am not sure but you can set the parameters in nginx config file for allowing CORS in all browsers.
This link can be a help where there is nginx config file is given to allow CORS

Resources