I have updated my gems and I have lost my old nginx config. I´m setting a new config in nginx.conf. My new Nginx version is 1.17.3. The home page is loading and navigation from home is also right. But, if I directly type a specific url in my browser, Nginx responds a 404.
I don´t remember what I´m missing. My nginx.conf file:
events {
worker_connections 1024;
}
http {
upstream api.development {
# Path to Puma SOCK file, as defined previously
server unix:/tmp/puma.sock fail_timeout=0;
}
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
# set client body size to 10M #
client_max_body_size 10M;
gzip on;
server {
listen 80 default_server;
listen [::]:80 default_server ipv6only=on;
root /path-to-root/app;
index index.html index.htm;
# Proxy requests to backend API
location /api {
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $host;
#proxy_set_header X-Forwarded-Proto https;
proxy_redirect off;
rewrite ^/api(.*) /$1 break;
proxy_pass http://api.development;
}
}
include servers/*;
}
I think I have just already got it by adding: try_files $uri $uri/ /index.html =404;
My concern is that with this line it´s working on development environment, however, in production, I haven´t updated the server yet, and I don´t see that I have any try_files enabled in my config file. So´I don´t understand if this is what I should do. I´m not skilled at all in nginx config.
Related
Those are the environment.
■Frontend
Server: Vercel
■Backend
Server: EC2
Middle: Nginx
note: wordpress is constructed in EC2
What I want is
① Accessing to https://example2.jp/hoge which is backend
② Redirecting to https://example.jp/hoge which is frontend to use Nginx showing wordpress page
I tried rewrite to achieve, but it couldn't work at all.
server {
listen 80;
server_name example2.jp;
root /var/www/example2/current/public;
location / {
proxy_pass http://example2;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_redirect off;
}
location /hoge/ {
root /var/www/html;
index index.php;
try_files $uri $uri/ /hoge/index.php?$args;
expires 7d;
rewrite ^ $scheme://example.jp/hoge/ permanent;
}
}
Could you give me tips?
I am very new with nginx and bitbucket server setup. I have an nginx server setup in front of bitbucket instance (running on port 7990). Below is my configuration of /etc/nginx/sites-available/default (not /etc/nginx/nginx.conf)
server {
listen 80 default_server;
listen [::]:80 default_server;
root /var/www/html;
index index.html index.htm index.nginx-debian.html;
server_name _;
location / {
# First attempt to serve request as file, then
# as directory, then fall back to displaying a 404.
try_files $uri $uri/ =404;
}
location /bitbucket/ {
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://127.0.0.1:7990/;
sendfile off;
expires 0;
add_header Cache-Control private;
add_header Cache-Control no-store;
add_header Cache-Control no-cache;
}
}
Now if I hit
http://my_ip/bitbucket
it forwards to
http://my_ip/bitbucket/setup
page, but fails to load css and js files.
When I see the html source of setup page, it includes some js/css files' absolute paths while importing (e.g. link type="text/css" rel="stylesheet" href="/s/277823dc49fd32854c324f87e345f7f6-CDN/-196139424/aef68c7/1/1b615eee93091f36273c8450f7d72556/_/download/contextbatch/css/_super/batch.css"). This seems like nginx is looking for the css files in "s" folder under its own directory.
I also checked that
http://my_ip/bitbucket/s/277823dc49fd32854c324f87e345f7f6-CDN/-196139424/aef68c7/1/1b615eee93091f36273c8450f7d72556/_/download/contextbatch/css/_super/batch.css
Url returns the page in browser.
Please let me know how to load css/js files. I also want to know why the css url is so ugly(it seems that it includes some random numbers).
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;
}
I have this in my file in my /etc/nginx/sites-available/
upstream app_server {
server 127.0.0.1:9000 fail_timeout=0;
}
server {
listen 80 default_server;
listen [::]:80 default_server ipv6only=on;
root /home/portforlio;
index index.html index.htm;
client_max_body_size 4G;
server_name khophi.co www.khophi.co;
keepalive_timeout 5;
location /media {
alias /home/portfolio/media;
}
location /static {
alias /home/portfolio/static;
}
location ~* \.(jpg|jpeg|png|gif|ico|css|js)$ {
expires 30d;
}
location / {
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_redirect off;
proxy_pass http://app_server;
}
}
With the configuration above, I'm told I've leveraged files caching when I check via https://developers.google.com/speed/pagespeed/insights/?url=http%3A%2F%2Fkhophi.co%2F&tab=desktop
Yet, when I visit the site, it doesn't load the css and I get an unstyled page.
What is missing? I have tried this and that, yet although I have also specified my root directory, it still doesn't show them when requested in the browser.
Mysteriously, pageinsights sees them and even as cached, how?
the live site is at khophi.co
The answer I figured out to be a typo.
Should be portfolio not portforlio
I added an error log to nginx and from there, I noticed it says the directory didn't exist, that prompted me to check the folder names
error_log /home/nginx/nginx_error.log warn;
this is my config:
server {
listen 80;
server_name ~^(?<sb>.+)\.a\.b\.c\.com$;
access_log /data/logs/nginx/tas.access.log main;
location / {
proxy_intercept_errors on;
proxy_pass http://b.c/a/$sb/;
proxy_set_header Host $host;
proxy_redirect off;
}
}
and browser report to many redirects.
If, as you say, you want to proxy to localhost:8082, you need to say so in the proxy_pass line:
server {
listen 80;
server_name ~^(?<sb>.+)\.a\.b\.c\.com$;
access_log /data/logs/nginx/tas.access.log main;
location / {
proxy_intercept_errors on;
proxy_pass http://localhost:8082/a/$sb/;
proxy_set_header Host $host;
proxy_redirect off;
}
}
Without all of the information, it's hard to guess what's going on. Based on the comments, my guess is that you are using virtual hosting so that the upstream site is also served by the same nginx. So this line is the problem:
proxy_set_header Host $host;
The nginx variable $host is pointing to the current Host header (which matches the server_name). So if you set the same host header for the upstream again, then nginx will find the same location block above because nginx relies on the Host header to find the proper server. Thus the redirect loop.
Set
proxy_set_header Host your_upstream_server_name
will fix it then.