React deployed in Nginx, AJAX Axios don't work - ajax

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/*;
}

Related

403 forbidden after installing Namecheap’s PositiveSSL

After installing SSL certificate with Namecheap’s PositiveSSL on my server i’m getting 403 forbidden error page. in HTTP the page does work.
I’m developing a Laravel app on a Nginx host
here’s my /etc/nginx/sites-enabled/ssl.conf
listen 443;
ssl on;
ssl_certificate /etc/nginx/sites-available/nginx_bundle_guida_deltipo.crt;
ssl_certificate_key /root/azas.social.key;
server_name azas.social;
access_log /var/log/nginx/nginx.vhost.access.log;
error_log /var/log/nginx/nginx.vhost.error.log;
location / {
root /var/www/visibilio/visibilio/public;
index index.html;
}
}
And the following is nginx.conf file:
##
# SSL Settings
##
client_max_body_size 1000m;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2; # Dropping SSLv3, ref: POODLE
ssl_prefer_server_ciphers on;
##
# Logging Settings
##
access_log /var/log/nginx/access.log;
error_log /var/log/nginx/error.log;
##
# Virtual Host Configs
##
include /etc/nginx/conf.d/*.conf;
include /etc/nginx/sites-enabled/*;
}
this is my etc/nginx/sites-available/
server {
listen 80;
listen [::];
root /var/www/visibilio/visibilio/public;
index index.php index.html index.htm;
# Make site accessible from http://localhost/
server_name in-visibili.org;
return 301 https://azas.social$request_uri;
location / {
# First attempt to serve request as file, then
# as directory, then fall back to displaying a 404.
try_files $uri /index.php?$query_string;
# Uncomment to enable naxsi on this location
# include /etc/nginx/naxsi.rules
}
error_page 404 /404.html;
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/run/php/php7.2-fpm.sock;
}
Any help would be highly appreciated

php file is not open with nginx-macOs

I have some issue with nginx configuration in macos can anyone help me
Here is my 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 80;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root /Users/jurde/www/;
index index.html index.htm index.php;
}
#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 $document_root$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/*;
}
If i open 127.0.0.1/index.php then it showing error
Here is error
Error Log
2019/06/12 21:39:12 [error] 4237#0: *3 kevent() reported that connect() failed (61: Connection refused) while connecting to upstream, client: 127.0.0.1, server: localhost, request: "GET /index.php HTTP/1.1", upstream: "fastcgi://127.0.0.1:9000", host: "127.0.0.1"
if i open 127.0.0.1/index.html then it's working fine
PHP version is 7.1 and it is working fine
i'm using Laravel project is working fine. but core php project is not open
please help me any help will appreciate

Can´t solve 403 Forbidden Error [nginx - windows]

I tried several times to solve my problem but I didn´t find any solution for my problem!
I can visit my website from my vps but if i try it from my local computer I get a 403 Error!
It would be nice if anyone can help me!
PS: I don´t want to show the files with autoindex!!!
My 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 80;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root html;
index index.html index.php;
allow all;
}
#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 C:/nginx/html/$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 localhost.crt;
ssl_certificate_key localhost.key;
#ssl_session_cache shared:SSL:1m;
ssl_session_timeout 5m;
ssl_ciphers HIGH:!aNULL:!MD5;
ssl_prefer_server_ciphers on;
}
}
Run as administrator fixed this problem for me.
I have the same problem. installed nginix using Chocolatey. This installs nginx as services too.
After facing this problem I stoped the services and started nginx as administrator.
Just right click the exe file and choose Run As Adminstrator.

NGINX MAGENTO v1.7 CSS + JS Not Working

I think NGINX isn't serving magento CSS & JS files from the correct location my root is */data/www/public_html/hotnstylish/public/* but nginx seems to think its /etc/nginx/html !?? the frontend and backend both load with no style at all any clues would be great!
Part of my NGINX Error Log:
2013/11/20 23:26:08 [error] 13982#0: *3 open() "/etc/nginx/html/js/calendar/calendar.js" failed (2: No such file or directory), client: 89.168.219.134, server: 836237.vps-10.com, request: "GET /js/calendar/calendar.js HTTP/1.1", host: "836237.vps-10.com", referrer: "http://836237.vps-10.com/index.php/admin/dashboard/index/key/d6a8576bd8743279b6812c60ccd6b464/"
2013/11/20 23:26:08 [error] 13982#0: *5 open() "/etc/nginx/html/js/calendar/calendar-setup.js" failed (2: No such file or directory), client: 89.168.219.134, server: 836237.vps-10.com, request: "GET /js/calendar/calendar-setup.js HTTP/1.1", host: "836237.vps-10.com", referrer: "http://836237.vps-10.com/index.php/admin/dashboard/index/key/d6a8576bd8743279b6812c60ccd6b464/"
My vhost config file is as follows (messy as hell):
server {
listen 80;
server_name 836237.vps-10.com;
root /data/www/public_html/hotnstylish/public/;
access_log /data/www/public_html/hotnstylish/log/access.log;
error_log /data/www/public_html/hotnstylish/log/error.log;
location / {
root /data/www/public_html/hotnstylish/public/;
index index.html index.htm index.php;
try_files $uri $uri/ #handler; ## If missing pass the URI to Magento's front handler
expires 30d; ## Assume all files are cachable
include /etc/nginx/mime.types;
}
# error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
# location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}
## These locations would be hidden by .htaccess normally
location ^~ /app/ { deny all; }
location ^~ /includes/ { deny all; }
location ^~ /lib/ { deny all; }
location ^~ /media/downloadable/ { deny all; }
location ^~ /pkginfo/ { deny all; }
location ^~ /report/config.xml { deny all; }
location ^~ /var/ { deny all; }
location ~ \.css {
add_header Content-Type text/css;
}
location ~ \.js {
add_header Content-Type application/x-javascript;
}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
location ~ ^/(app|includes|lib|media/downloadable|pkginfo|report/config.xml|var)/ { internal; }
location /var/export/ { internal; }
location /. { return 404; }
location #handler { rewrite / /index.php; }
location ~* .php/ { rewrite ^(.*.php)/ $1 last; }
location ~* .php$ {
if (!-e $request_filename) { rewrite / /index.php last; }
expires off;
root /data/www/public_html/hotnstylish/public/;
fastcgi_pass 127.0.0.1:9000;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param MAGE_RUN_CODE default;
fastcgi_param MAGE_RUN_TYPE store;
include fastcgi_params;
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}
}
My Ngingx.conf:
user nginx;
worker_processes 4;
error_log /var/log/nginx/error.log warn;
pid /var/run/nginx.pid;
events {
worker_connections 1024;
}
http {
#include /etc/nginx/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 /var/log/nginx/access.log main;
sendfile on;
autoindex off;
map $scheme $fastcgi_https { ## Detect when HTTPS is used
default off;
https on;
}
keepalive_timeout 10;
gzip on;
gzip_comp_level 2;
gzip_proxied any;
gzip_types text/plain text/html text/css application/x-javascript text/xml application/xml application/xml+rss text/javascript;
# Load config files from the /etc/nginx/conf.d directory
#include /etc/nginx/conf.d/*.conf;
#Load vHosts file for each site separately
include /etc/nginx/sites-enabled/*;
}
Make sure that your request processed in right server_name context.
If you use nginx ≥0.8.21 try to set
listen 80 default_server;
server_name 836237.vps-10.com;

Nginx Setup on Ubuntu

I'm trying to setup nginx with passenger to work on Ubuntu with RVM.
I should get my apps home page when i go to localhost, instead I recieve the default nginx home page.
user antarr;
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 {
passenger_root /home/antarr/.rvm/gems/ruby-1.9.2-p180#myapplication/gems/passenger-3.0.7;
passenger_ruby /home/antarr/.rvm/wrappers/ruby-1.9.2-p180#myapplication/ruby;
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 80;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root html;
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;
#}
}
# HTTP SERVER
server {
listen 80;
server_name localhost;
root /rails_apps/Blog/public; # <--- be sure to point to 'public'!
passenger_enabled on;
}
}
This is because you've defined two servers which are listening on port 80. Remove the first one and it should work.

Resources