proxy_pass not support # in nginx - linux

I am new to nginx and I am trying to create clean URL pattern where if I use below URL on browser "http://xx.xx.xx.xx:61001/employee" then it should route to "http://localhost:8080/emp/#/details". But unfortunately I am getting 404 error on browser. Even though my application is up and running. # (special character) having some issue in nginx. can someone help me here.
Below my configuration:
location /employee {proxy_pass http://localhost:8080/emp/#/details;
}
Getting 404 error on browser
This is my full server config:
server {
listen 8081;
server_name xx.xx.xxx.xxx;
location / { root html; index index.html index.htm; }
location /employee { proxy_pass localhost:8080/emp/#/details; }
error_page 500 502 503 504 /50x.html;
location = /50x.html { root html; }
}

Now you are listening on port 8081 change it to 80.
server {
listen 80;
server_name xx.xx.xxx.xxx;
location / { root html; index index.html index.htm; }
location /employee { proxy_pass localhost:8080/emp/#/details; }
error_page 500 502 503 504 /50x.html;
location = /50x.html { root html; }
}

Related

Nginx access to images in backend folder

My app store images on the backedn side in nodejs folder: /images.
After publish it to Ec2 I don't have an access to these images by url like: www.domain.com/image01.jpg
Configuration in /etc/nginx/sites-available/default:
server {
charset utf-8;
listen 80 default_server;
server_name _;
location / {
root /opt/front-end;
try_files $uri /index.html;
}
location /images/ {
root /opt/back-end/;
}
location /api/ {
proxy_pass http://localhost:4000/;
}
}
Is it the configuration problem or permission or something else? I will be grateful for help!
The correct configuration should be:
server {
charset utf-8;
listen 80 default_server;
server_name _;
location / {
root /opt/front-end/;
try_files $uri /index.html;
}
location /api {
proxy_pass http://localhost:4000;
}
location /images {
autoindex on;
alias /opt/back-end/images/;
}
}

Nginx location /yyy redirected to root location

I have 2 locations in my nginx conf like so :
upstream server {
server 127.0.0.1:XXXX;
}
server {
listen 443 ssl;
server_name example.com;
ssl_certificate XXXX;
ssl_certificate_key XXXX;
location /yyy {
proxy_pass http://server/YYY/;
...
}
location / {
proxy_pass http://server/XXX/;
...
}
}
But it seems that https://example.com/yyy redirects to http://server/XXX/ instead of http://server/YYY/ as if nginx considered it as a subpath of mydomain.com and not a different location.
However if I try to access https://example.com/yyy/ then it redirects to http://server/YYY/.
I would like that both https://example.com/yyy/ and https://example.com/yyy redirect to http://server/YYY/

Nginx handle 500 internal server error security issue

I am trying to fix a security vulnerability of 500 internal server error disclose location of the file
My issue is similar to that of (https://cdn-images-1.medium.com/max/1600/1*2DAwIEJhgLQd82t5WTgydA.png)
(https://medium.com/volosoft/running-penetration-tests-for-your-website-as-a-simple-developer-with-owasp-zap-493d6a7e182b)
I am tried with
proxy_intercept_errors on;
and
error_page 500
redirection but it didnt help.
Any help on this ?
This is a basic example of implementing proxy_intercept_errors on;
upstream foo {
server unix:/tmp/foo.sock;
keepalive 60;
}
server {
listen 8080 default_server;
server_name _;
location = /errors/5xx.html {
internal;
root /tmp;
}
location / {
proxy_pass http://foo;
proxy_http_version 1.1;
proxy_redirect off;
proxy_set_header Host $http_host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Real-IP $remote_addr;
proxy_intercept_errors on;
error_page 500 501 502 503 504 505 404 =200 /errors/5xx.html;
}
}
Notice the:
error_page 500 501 502 503 504 505 404 =200 /errors/5xx.html;
This will intercept some 5xx errors and the 404 except and return with a 200
Also, check the /errors/5xx.html location, is using root /tmp; therefore you still need to create the file errors/5xx.html:
$ mkdir /tmp/errors
$ echo "intercepting errors" > /tmp/errors/5xx.hml
You don't necessarily need to a file to reply you request you could also use something like this:
location = /errors/5xx.html {
internal;
default_type text/plain;
return 200 'Hello world!';
}
In your case the 404 File not found could be handle different, for example:
upstream failover{
server server2:8080;
}
server {
listen 80;
server_name example.com;
root /tmp/test;
location ~* \.(mp4)$ {
try_files $uri #failover;
}
location #failover {
proxy_pass http://failover;
}
}
In this case if the file ending with .mp4 not found it will try another server, then if required you still can intercep the error there.

Proxy Passing Socket.IO connections on nginx not working

I am trying to proxy pass a node.js-socket.io app with nginx.
The client is an html file with some javascript in it;
<html>
<head>
<script src="socket.io.js"></script>
<script>
var socket = io('http://localhost:80');
socket.on('welcome', function(data){
console.log('Server says:' + data);
socket.emit('client-response', 'thank you!');
});
</script>
</head>
<body>
Socket.io
</body>
</html>
And the server block that supposed to proxy pass in nginx.conf file is this;
server {
listen 80;
listen [::]:80;
#root /usr/share/nginx/html;
include /etc/nginx/default.d/*.conf;
location / {
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "Upgrade";
proxy_pass "http://localhost:2156";
}
error_page 404 /404.html;
location = /40x.html {
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
}
}
I have my node.js app up and running in port "2156".
When I test this, the client tries to reach the socket.io on the port 80 and fails with a 404 error (Because nginx was supposed the pass the proxy to the port 2156 but it didn't).
What am I missing here?
Edit: I've changed the client to connect at "http://localhost/socket.io/" and rewrote the nginx.conf like this:
server {
listen 80 default_server;
listen [::]:80 default_server;
root /usr/share/nginx/html;
location /socket.io/ {
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "Upgrade";
proxy_set_header Host $host;
proxy_pass http://localhost:2156/socket.io/;
}
error_page 404 /404.html;
location = /40x.html {
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
}
}
And it worked.

Nginx cannot open the page

I just build a web server using nginx and i try to change the root path but it failed. It doesnt open the page instead download a file. Also the extension of the file was not specified. Sorry for my english. Anyone can help me to fix this? here it is my sites-available/default:
# You may add here your
# server {
# ...
# }
# statements for each of your virtual hosts
server {
listen 80;
server_name localhost;
access_log /var/log/nginx/localhost.access.log;
location / {
root /var/www/rahma/wordpress;
# root /var/www/rahma;
index index.php index.html index.htm;
}
location /doc {
root /usr/share;
autoindex on;
allow 127.0.0.1;
deny all;
}
location /images {
root /usr/share;
autoindex on;
}
#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 /var/www/rahma/wordpress;
# root /var/www/rahma;
}
# 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$ {
#fastcgi_pass 127.0.0.1:9000;
#fastcgi_index index.php;
#fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
#includefastcgi_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;
#server_name localhost;
#ssl on;
#ssl_certificate cert.pem;
#ssl_certificate_key cert.key;
#ssl_session_timeout 5m;
#ssl_protocols SSLv2 SSLv3 TLSv1;
#ssl_ciphers ALL:!ADH:!EXPORT56:RC4+RSA:+HIGH:+MEDIUM:+LOW:+SSLv2:+EXP;
#ssl_prefer_server_ciphers on;
#location / {
#root html;
#index index.html index.htm;
#}
#}
Before set the root to wordpress, i just add index.php on line 'index' and create a file php.info but it doens't work too.

Resources