Configure local nginx as reverse proxy - macos

I'm running a nginx on my local machine (macOS) and I want to use it as a reverse proxy. Calling my.local should serve me http://192.168.178.60:8000.
But it looks like as there is a syntax error in my config file. Is it wrong to use two server blocks? If I remove the second one, I can call localhost:8080 and see the default output. If I add the custom server block, I cannot call localhost:8080.
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
server {
listen 8080;
server_name localhost;
location / {
root html;
index index.html index.htm;
}
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
server {
listen my.local:80;
server_name my.local;
location / {
proxy_pass http://192.168.178.60:8000;
}
}
include servers/*;
}

Related

Why does this 404 time out?

Can someone explain why it can take a page 30 seconds to timeout? According to Chrome's debugger it is pending on a 404.
And that I don't get. If it is a 404, why doesn't it return page not found right away then?
The nginx config in question looks like this
# redirect http to https
server {
listen ${API_PORT} default_server;
listen [::]:${API_PORT} default_server;
server_name example-api.${SITE_SUFFIX};
return 301 https://$host$request_uri;
}
server {
set $indexhtml 'index.html';
# port to example on. Can also be set to an IP:PORT
listen 8443 ssl;
# sets the domain[s] that this vhost server requests for
server_name example.${SITE_SUFFIX};
ssl_certificate /certs/example.${SITE_SUFFIX}/fullchain.pem;
ssl_certificate_key /certs/example.${SITE_SUFFIX}/privkey.pem;
proxy_ssl_ciphers HIGH:!aNULL:!MD5;
client_max_body_size 4G;
keepalive_timeout 10;
if ($request_method !~ ^(GET|HEAD|PUT|PATCH|POST|DELETE|OPTIONS)$ ){
return 405;
}
root /var/www/frontend/public;
error_page 404 #404;
include conf.d/sites/content.conf;
# error page location redirect 302
location #404 {
return 302 /404;
}
}
server {
# port to example on. Can also be set to an IP:PORT
listen 8443 ssl;
# sets the domain[s] that this vhost server requests for
server_name sl.example.${SITE_SUFFIX};
ssl_certificate /certs/sl.example.${SITE_SUFFIX}/fullchain.pem;
ssl_certificate_key /certs/sl.example.${SITE_SUFFIX}/privkey.pem;
proxy_ssl_ciphers HIGH:!aNULL:!MD5;
client_max_body_size 4G;
keepalive_timeout 10;
if ($request_method !~ ^(GET|HEAD|PUT|PATCH|POST|DELETE|OPTIONS)$ ){
return 405;
}
error_page 404 #404;
include conf.d/sites/sh.conf;
# error page location redirect 302
location #404 {
return 302 /404;
}
}
Update
This is how it looks after the timeout.

React deployed in Nginx, AJAX Axios don't work

I want to combinate frontend with backend on the cloud virtual machine like DigitalOcean or AWS or Microsoft Azure.
Frontend: React
Backend: Spring Spring MVC Mybatis + Tomcat (NOT Spring Boot)
So I have searched for some methods using Nginx on the Internet. Firstly, I am going to combinate the frontend with backend on the local (macOS). Secondly, if the method succeeded, I will deploy the project on the cloud host. But, it occurs an error in the first step.
When I typed the localhost:8081 site, the browser can show the normal website. But no any backend data. I have supposed that the Nginx configuration may be inappropriate for access to the backend.
Maybe add proxy_pass http://localhost:8080/example_war_exploded/; in nginx.conf, but I got the JSP content without any CSS or JavaScript. I only want to access data by AJAX Axios.
Related files
nginx.conf
#user nobody;
worker_processes 1;
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#pid logs/nginx.pid;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
#log_format main '$remote_addr - $remote_user [$time_local] "$request" '
# '$status $body_bytes_sent "$http_referer" '
# '"$http_user_agent" "$http_x_forwarded_for"';
#access_log logs/access.log main;
sendfile on;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
#gzip on;
server {
listen 8081;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root html/build;
index index.html index.htm;
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
#location ~ \.php$ {
# root html;
# fastcgi_pass 127.0.0.1:9000;
# fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
# include fastcgi_params;
#}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}
# another virtual host using mix of IP-, name-, and port-based configuration
#
#server {
# listen 8000;
# listen somename:8080;
# server_name somename alias another.alias;
# location / {
# root html;
# index index.html index.htm;
# }
#}
# HTTPS server
#
#server {
# listen 443 ssl;
# server_name localhost;
# ssl_certificate cert.pem;
# ssl_certificate_key cert.key;
# ssl_session_cache shared:SSL:1m;
# ssl_session_timeout 5m;
# ssl_ciphers HIGH:!aNULL:!MD5;
# ssl_prefer_server_ciphers on;
# location / {
# root html;
# index index.html index.htm;
# }
#}
include servers/*;
}
Backend, Tomcat URL: http://localhost:8080/example_war_exploded/
Spring MVC Controller
#Controller
#RequestMapping(value = "/comment")
public class CommentController {
#GetMapping(value = "/comments")
}
Frontend Axios
axios.get(`/comment/comments`).then((response) => {
this.setState({
comments: response.data,
});
});
What should I do... Thanks.
Usually, if you want to get JSON from the backend, we need to access http://localhost:8080/example_war_exploded/comment/comments.
So, we should add the code snippet in nginx.conf.
location ^~ /comment {
proxy_pass http://localhost:8080/example_war_exploded/comment;
}
Completed code in nginx.conf
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile off;
keepalive_timeout 65;
server {
listen 8081;
server_name localhost;
location / {
root html/build;
index index.html index.htm;
}
# New
location ^~ /comment {
proxy_pass http://localhost:8080/example_war_exploded/comment;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
include servers/*;
}

Nginx serving non-secure resources on https domain

I'm having some issues setting up a server with an SSL certificate. I was able to install the certificate just fine and restarted the nginx service. However, when I attempt to load my website, I see that all img, css and js files are being retrieved with http instead of https. This is a Magento website. Is there something wrong with my conf file?
server {
listen 80;
server_name www.my-domain.com;
return 301 $scheme://my-domain.com$request_uri;
}
server {
listen 80;
listen 443 ssl;
ssl_certificate /etc/ssl/my-domain/my-domain_com.pem;
ssl_certificate_key /etc/ssl/my-domain/my-domain_com.key;
access_log /var/log/nginx/magento.local-access.log;
error_log /var/log/nginx/magento.local-error.log;
server_name my-domain.com;
root /var/www/my-domain;
include conf/magento_rewrites.conf;
include conf/magento_security.conf;
# PHP handler
location ~ \.php {
## Catch 404s that try_files miss
if (!-e $request_filename) { rewrite / /index.php last; }
## Store code is defined in administration > Configuration > Manage Stores
fastcgi_param MAGE_RUN_CODE default;
fastcgi_param MAGE_RUN_TYPE store;
# By default, only handle fcgi without caching
include conf/magento_fcgi.conf;
}
# 404s are handled by front controller
location #magefc {
rewrite / /index.php;
}
# Last path match hands to magento or sets global cache-control
location / {
## Maintenance page overrides front controller
index index.html index.php;
try_files $uri $uri/ #magefc;
expires 24h;
}
rewrite ^/minify/([0-9]+)(/.*.(js|css))$ /lib/minify/m.php?f=$2&d=$1 last;
rewrite ^/skin/m/([0-9]+)(/.*.(js|css))$ /lib/minify/m.php?f=$2&d=$1 last;
location /lib/minify/ {
allow all;
}
}
maybe cause this is how they are called, if you need to serve every thing as https, i would create an empty server that listens on 80 and redirect to https
server {
# listen 80; delete this part
listen 443 ssl;
# the rest of the config
}
# add this server
server {
listen 80;
server_name example.com;
location / # or a more specific location '~ \.(jpg|css|js|jpeg|png|gif)' {
return https://example.com$request_uri;
}
}
or just fix the css location, it might be an absolute URL with http

Nginx proxy_pass is ignored if static file exists

I'm trying to get nginx to always proxy certain requests, even if a static file exists. I have the proxying working fine, except nginx seems to insist on serving a static version of the file even if a proxy directive has been declared.
So in the config below, if a file named "/siteroot/static/members/page.html" existed, it would be (incorrectly) served directly instead of proxying, but if I remove the file, then proxying proceeds as expected. How can I force nginx to always proxy?
I'm running nginx 0.7.67, here's the full config:
worker_processes 1;
error_log logs/error.log;
pid logs/nginx.pid;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
access_log logs/access.log;
sendfile on;
keepalive_timeout 65;
tcp_nodelay on;
gzip on;
gzip_disable "MSIE [1-6]\.(?!.*SV1)";
}
server {
listen 80;
server_name .XXX.net .XXXnet.net;
server_name_in_redirect off;
location ^~ /members {
access_log logs/members-access.log;
proxy_pass http://127.0.0.1:5010;
}
location ^~ /search {
access_log logs/search-access.log;
proxy_pass http://127.0.0.1:5010;
}
location / {
root /siteroot/static;
}
# redirect server error pages to the static page /50x.html
error_page 404 %(ROOT)s/web/XXX/public/404.html;
error_page 500 502 503 504 %(ROOT)s/web/XXX/public/50x.html;
}
After turning on nginx debugging, it turns out that nginx is correctly reverse-proxying to the back-end application. It is the back-end Pylons application that is serving the static file as-is instead of executing a controller. Nginx seems to be functioning properly.

ngnix to proxy server B only if got 404 on server A

I'm trying to configure nginx (0.7.65) so it'll proxy to server A, and if it gets 404 will try to proxy to server B.
I've tried the following, but it doesn't work. Any ideas?
server {
error_log /tmp/nginx.error.log;
access_log /tmp/nginx.access.log;
listen 4433;
server_name localhost;
location / {
proxy_pass http://localhost:5984;
error_page 404 = #fallback;
}
location #fallback {
proxy_pass http://localhost:5983;
}
}
proxy_intercept_errors on;

Resources