Nginx proxy return 404 with Laravel and Vue - laravel

i have a problem with nginx and laravel/vue app, hope yours help
I have two web app call Admin and System.
Admin use Vuejs call api to System using Nginx proxy (image bellow)
server {
listen 80;
server_name admin.abc.com;
location / {
root /usr/share/nginx/html/onestudy-vuejs/dist;
index index.html;
try_files $uri $uri/ /index.html;
}
location /api/ {
proxy_pass https://system.abc.com;
}
location ~ /\.ht {
deny all;
}
}
After some day not access to web, i have error such as when access api from admin domain (example: https://admin.abc.com/api/user_info)
{"error":{"name":"Error","status":404,"message":"There is no method to handle ","statusCode":404}}
every request 3 times, 2 times ok and 1 time i see this error
but if access direct from system admin, everything will be still ok
After this, i just restart nginx, i don't see this error??? :( don't understand, don't know exactly what the cause is ##
Note: My project has two env, staging and prod, but only prod has error (same config nginx between two env)
Some info of project
Tech stack: Laravel, Vue
Webserver: Nginx
Cloud: AWS (EC2 + ALB)
I see this topic nginx proxy_pass 404 error, don't understand why but it's not for my problem
Glad for your support.
If u need info of system for anwser, please request bellow. Tks u

Related

Custom nginx configuration for Laravel in AWS Elastic Beanstalk

Hi I am using AWS elastic beanstalk for my Laravel application
When I first uploaded my application it is not showing the login page, 404 error so I tried with app_url/index.php/login and it worked
This is how I figure out I need to add this to my Nginx configuration, I manually added this, ssh into ec2 instance, and worked
location / {
try_files $uri $uri/ /index.php?$query_string;
}
but every time I deploy my new code it replaces my manual code
I also read https://docs.aws.amazon.com/elasticbeanstalk/latest/dg/java-tomcat-proxy.html?icmpid=docs_elasticbeanstalk_console this doc and created this folder structure in my application root folder
.ebextensions
nginx
conf.d
elasticbeanstalk
my-server-conf.conf
Seems not working for me
Please let me know what should I put In my-server-conf.conf file and what should I do so it works for every deploy
You should be using .platform, not .ebextensions as explained in aws docs. For example:
.platform/nginx/conf.d/elasticbeanstalk/laravel.conf
location / {
try_files $uri $uri/ /index.php?$query_string;
}

How do you use basic_auth for all but one route using Laravel Forge

I am using the in-built HTTP basic access authentication for Laravel Forge.
I am currently using basic_auth for all routes.
I would like to exclude one route (basic_auth off).
I tried to customise the file, example: /etc/nginx/forge-conf/.../server/.htpasswd-{ruleId}
I added the following additional code:
location = /example {
basic_auth off;
}
I also tried:
location /example {
basic_auth off;
}
I restarted nginx. I restarted the server. Same result: no change. The route /example was not excluded from basic_auth.
I was unable to find a solution using the in-built "Security" tool for basic authentication provided by Laravel Forge.
I was able to solve the problem by updating the nginx file for the site without using the aforementioned "Security" tool.
Replace www.example.com with your site address on Laravel Forge.
Replace /example with the path that you would like to exclude.
location / {
auth_basic "Restricted Access";
auth_basic_user_file /home/forge/www.example.com/.htpasswd;
try_files $uri $uri/ /index.php?$query_string;
}
location /example {
auth_basic off;
try_files $uri $uri/ /index.php?$query_string;
}
I created a .htpasswd file (search Google to find out how to generate your own with a username and password). I uploaded it to the root of my project directory, at the same level as the .env file.

Nginx configuration setup for windows

I am trying to run my vue applications trough nginx on windows and i was using the following tutorials, one to run nginx with AlwaysUp and the other one to configure it.
https://www.coretechnologies.com/products/AlwaysUp/Apps/RunNginxAsAService.html
https://graspingtech.com/nginx-virtual-hosts-ubuntu-16-04/
I also stumbled upon the following stack overflow question which is basically the problem i have but it didnt work:
nginx Windows: setting up sites-available configs
The service is running and recognizes the two domains i am trying to set up but for whatever reason it always sends me back to the NGINX Welcome page and i am not sure what i am doing wrong.
I followed the steps on the second tutorial and did a few changes, such ass adding "server_names_hash_bucket_size 64;" to my nginx.config file. I also created the symlink between the "sites-available" and "sites-enabled directories" using windows mklink.
Here are my files.
Nginx.config
worker_processes 1;
events {
worker_connections 1024;
}
http {
server_names_hash_bucket_size 64;
include mime.types;
default_type application/octet-stream;
include "C:/nginx/nginx/sites-available/*.conf";
sendfile on;
server {
listen 80;
server_name localhost;
location / {
root html;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
}
My app config file in sites-available which also contains a symlink in sites-enabled:
server {
listen 80;
listen [::]:80;
server_name myapp.nginx.br;
root "C:/Users/Documents/git-repository/my-app/dist";
index index.html;
location / {
try_files $uri $uri/ #rewrites;
}
location #rewrites {
rewrite ^(.+)$ /index.html last;
}
location ~* \.(?:ico|css|js|gif|jpe?g|png)$ {
expires max;
add_header Pragma public;
add_header Cache-Control "public, must-revalidate, proxy-revalidate";
}
}
I also faced the issue how to kill the nginx process. I stumbled a while until I came up with the following command which works:
taskkill /F /FI "IMAGENAME eq nginx.exe"
Well, after many hours i actually found out what was happening.
Everything in my files, from the nginx.conf file to every other .conf files were ok.
However i discovered that when restarting or stop/starting my nginx service with either the AlwaysUp program or the windows service options the service would still be active somehow and wouldnt apply my changes, therefore it would always show me the "welcome to nginx" page.
So i just restarted my computer because i wasnt able to kill the service with conventional means and it worked just fine for every single app i have.
I am sure there is a better way to kill the service and restart it but restarting my computer so the changes to my files would actually be applied solved it.
EDIT: I also discovered that windows takes a little while to stop the nginx service, so if you are using always up try stopping the service there, if it fails try stopping the service trough windows services menu. Also don't forget to set it to manual so you wont accidentally access you nginx app instead of your actual deployed app.

404 running Mvc3 with Mono on Raspberry Pi with Nginx and FastCgi

After lots and lots of hours put into configuring, formatting, reformatting, googling and reading what seems like hundred tutorials I still havent been able to run a MVC3 application on Raspberry Pi.
When I try to access it it throws a 404 error regardless if I try to access it via http://network_ip or http://network_ip/Home/Index
(source: shrani.si)
It does the same whether accessed from Nginx or XSP4 server.
My configuration:
nginx
server {
listen 80; ## listen for ipv4; this line is default and implied
root /var/www;
# Make site accessible from http://localhost/
server_name localhost;
access_log /var/log/nginx/localhost.log;
location / {
root /var/www/;
fastcgi_pass 127.0.0.1:9000;
#fastcgi_index Home/Index;
include /etc/nginx/fastcgi_params;
}
}
MVC3 app should be deployed correctly (deleted Entity Framework references, copied Mvc references).
Im running Mono 2.10.8.1
Thank you

nginx config files redirecting to subfolder

I'm currently trying to deploy a website in two directories. Despite the lot of documentations on it, I was not able to obtain the behavior that I wish. This is the architecture of the websites :
The main website page is stored in /opt/www/mainsite/index.html
The second webiste (working with CodeIgniter) is stored in /opt/www/apps/myapp/index.php
I wish configure nginx to obtain this behavior :
All requests in http must be redirect to https
www.mydomain.com must point to /opt/www/mainsite/index.html
www.mydomain.com/myapp must point to /opt/www/apps/myapp/index.php
currently, my config file contains :
# redirect http to https
server {
listen 80;
rewrite ^(.*) https://$host$1 permanent;
}
# main webiste
server {
listen 443;
# ssl elements...
root /opt/www/mainsite;
index index.html;
server_name www.mydomain.com;
location / {
try_files $uri $uri/ /index.html;
}
}
On this page I found all the informations to set the config file for CodeIgniter. But I don't know how to create the rules to point mydomain.com/myapp on the CodeIgniter folder and how to configure CodeIgniter in order to set the right configuration.
Is anybody can help me?
thanks in advance
You need http://wiki.nginx.org/HttpFastcgiModule to setup CodeIgniter.
Using 2 server blocks is better than using if block for redirect. See IF is Evil.
Don't use $host because that variable value is obtained from the request's HOST header, and can be easily faked. Always set a server_name directive and use that name instead.
Using "return 301" directive is better than a rewrite. Saving cpu time (regex is slow) and easy to follow. Note that a 302 redirect (rewrite...redirect) has side effect because 302 will turn all POST requests to GET requests, which is not good in your case.
You don't need try_files in the main site because the main site just serves static files. But you can use 'expires' directive to allow browser to cache the static files.
server {
listen 80;
server_name www.mydomain.com;
return 301 https://$server_name$request_uri;
}
server {
listen 443;
server_name www.mydomain.com;
# ssl elements...
location / {
root /opt/www/mainsite;
index index.html;
expires max;
}
location /myapp {
root /opt/www/apps/myapp;
# fastcgi module goes here...
}
}
server {
listen 80;
listen 443 ssl;
…
if ($scheme != "https") {
rewrite ^ https://$server_name$request_uri? redirect;
}
root /opt/www/mainsite/;
location /myapp {
root /opt/www/apps/myapp/;
}
}
You'd put whatever configuration that is necessary for your myapp within the location for myapp.
BTW, it is generally a bad idea to host multiple independent apps within a single Host, because of XSS concerns.

Resources