I have a PHP application(docker) + nginx. This server I access through https. But my problem is, I have the same application(updated version, same routes) in another server that I don't have an https or a public address. Is it possible for nginx to "redirect" to this new server but using the url of my current application/server until I don't have a public ip?
etc/hosts file
172.14.0.3 app // docker ip
# xx.xx.x.xxx app // new server ip
127.0.0.1 my_app_url.com
# xx.xx.x.xxx my_app_url.com // this is the new server IP I was testing
my nginx file, on the proxy_pass property, I was passing the new server ip
server {
listen 80;
server_name my_app_url.com;
location / {
proxy_set_header HOST $host;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass http://app;
#proxy_pass http://xx.xx.x.xxx; -> I was testing
}
}
server {
listen 443 ssl http2;
server_name my_app_url.com;
ssl on;
...
...
...
ssl_dhparam /etc/nginx/ssl/dhp-2048.pem;
location /.well-known/acme-challenge {
root /<app-path>
}
location / {
proxy_set_header HOST $host;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass http://app;
#proxy_pass http://xx.xx.x.xxx;
}
}
I tried to make these alterations, but I don't know if it's possible and acceptable to do this.
Related
I deployed an application in docker, which is a php application with apache2.
There is an nginx in docker, used to proxy to applications.
www.myhost.com -> nginx(docker) -> my application(docker)
If I request my application through host:port, it runs successfully.
When I request my application through domain name, it gets ERR_CONNECTION_RESET randomly.
net::ERR_CONNECTION_RESET 200 (OK)
server {
listen 80;
server_name myhost.com;
client_max_body_size 5120m;
location / {
#client_max_body_size 1024M;
#proxy_redirect off;
#proxy_set_header Host $http_host;
#proxy_set_header X-Real-IP $remote_addr;
#proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
#proxy_request_buffering off;
#proxy_buffering off;
proxy_redirect off;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Host $server_name;
proxy_pass http://192.168.17.19:8080/;
}
}
Use this config for Nginx server to use Nginx as a reverseproxy
upstream myhost {
server <docker-container-hostname/IP-address>:<port>
}
server {
listen 80;
server_name myhost.com;
location / {
proxy_pass http://myhost;
}
}
I have setup a PHP application running on a RHEL server with the following configuration and also made a few changes in the nginx.conf file, the application is running fine.
magento.conf
upstream fastcgi_backend {
server unix:/run/php-fpm/magento.sock;
}
server {
listen 80;
set $MAGE_ROOT /var/www/magento2;
set $MAGE_MODE developer;
access_log /var/log/nginx/magento-access.log;
error_log /var/log/nginx/magento-error.log;
include /var/www/magento2/nginx.conf.sample;
}
I have another nginx server that i am trying to use as a reverse proxy to the PHP application running on the remote server. The issue i am facing is when i load the site from my Nginx IP, the URL in browser changes to the IP of PHP application server.
I want the IP to remain same as of the Nginx server instead of changing to IP of PHP Application server
The configuration file for reverse proxy is below
reverse.conf
server {
listen 80;
#server_name mydomain.com;
location / {
access_log off;
proxy_pass http://10.128.0.10:80;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header Host $host;
proxy_redirect off;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
Please help me. Thanks
Without Lua you can try to edit your proxy_redirect off; line to proxy_redirect http://10.128.0.10:80 http://my.domain.com:80;.
Maybe repeat that line with a slash at the end and/or https at the beginning (port 443 instead of 80).
https://unix.stackexchange.com/a/291007/239596
With Lua:
server
{
listen 80;
# server_name example.com;
# resolver 127.0.0.11:53; # Docker-DNS. Needed for proxy_pass with variables inside!
set $MY_SCHEME http;
set $MY_HOST example.com;
set $target "${MY_SCHEME}://${MY_HOST}";
location /
{
rewrite_by_lua_block
{
local map = {
GET = ngx.HTTP_GET,
POST = ngx.HTTP_POST,
}
ngx.req.read_body()
local res = ngx.location.capture('/example123' .. ngx.var.request_uri, {
method = map[ngx.var.request_method],
body = ngx.var.request_body
})
-- Detect/change redirect...
local redirect_target = res.header.Location
if redirect_target and res.status > 300 and res.status < 309 then
-- ngx.log(ngx.ALERT, 'redirect_target: '..redirect_target)
local redirect_target_changed, n, err = ngx.re.gsub(redirect_target, '^https?[:][/][/]10[.]128[.]0[.]10', ngx.var.target)
-- ngx.log(ngx.ALERT, 'redirect_target_changed: '..redirect_target_changed)
return ngx.redirect(redirect_target_changed, 303)
elseif res.status == 500 then
return ngx.exit(500)
else
ngx.exec('#example123_normal')
return ngx.exit(ngx.HTTP_OK)
end
}
}
location ~ ^/example123(.*)$
{
access_log off;
proxy_pass http://10.128.0.10:80$1$is_args$args;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header Host $host;
proxy_redirect http://10.128.0.10/ $target/;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header X-Real-IP $remote_addr;
}
location #example123_normal
{
access_log off;
proxy_pass http://10.128.0.10:80$request_uri;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header Host $host;
proxy_redirect http://10.128.0.10/ $target/;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header X-Real-IP $remote_addr;
}
}
Totally untested. And if you can't do Lua and RegEx, it gets hard.
I am using nginx to reverse proxy my incoming requests into my docker containers running on local bound ports (e.g. 127.0.0.1:23123).
My following script got a problem:
server {
server_name me-and-my-problem.sample.zone;
location ~* ^/admin/ {
rewrite ^/admin/(.*) /$1 break;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_pass http://127.0.0.1:30024;
}
location / {
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_pass http://127.0.0.1:30023;
}
listen 80;
listen [::]:80;
}
Any traffic on the path admin/ should be redirected to my phpmyadmin container. Anything else to my app container but currently, the 404 message of my app container will be thrown.
Followed this link to configure Nginx click here
Tried to connect WebSocket(socketo.me) through HTTPS, didn't succeed gave the error as
WebSocket opening handshake timed out
As my hosting server's ngnix (version: 1.13.8) is configured to work in reverse proxy mode in the front-end. Here is the configuration of the Nginx
http {
map $http_upgrade $connection_upgrade {
default upgrade;
'' close;
}
upstream websocket {
server xx.xxx.xxx.x:8282; #External IP address
}
server {
location / {
proxy_pass http://xx.xxx.xxx.x:8080; #External IP address
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection $connection_upgrade;
proxy_read_timeout 120s;
}
}
}
chatroom.php
<script type="text/javascript">
$(document).ready(function(){
var conn = new WebSocket('ws://xx.xxx.xxx.x:8282');
conn.onopen = function(e) {
console.log("Connection established!");
};
conn.onmessage = function(e) {
console.log(e.data);
...
};
conn.onclose = function(e) {
console.log("Connection Closed!");
}
})
</script>
server.php
<?php
use Ratchet\Server\IoServer;
use Ratchet\Http\HttpServer;
use Ratchet\WebSocket\WsServer;
use MyApp\Chat;
require dirname(__DIR__) . '/vendor/autoload.php';
$server = IoServer::factory(
new HttpServer(
new WsServer(
new Chat()
)
),
8282
);
$server->run();
Before applying SSL,
Then after applying SSL,
Established the WebSocket connection via port #8282 from the terminal as shown below
root#user:/var/www/vhosts/somedomain.xy/httpdocs/chatroom-php-mysql/bin# php server.php
Server Started.
New connection! (84)
Connection 84 has disconnected
When website URL is opened in browser basically this is what it happens as follows:
Client request comes to front-end Nginx asking for some resource (.html page, .php page, image, javascript, etc). Nginx in our hosting server works on TCP ports: 80 - http, 443 - https.
Nginx checks if it has the resource already in its cache.
If the resource is cached, Nginx returns the cached content.
If the resource is not cached or if the dynamic page (e.g. index.php) is requested, Nginx proxies (forwards) the request to back-end server - Apache. Apache in our hosting server works on TCP ports: 7080 - http, 7081 - https. Then Nginx caches static content - HTML, images, js, css.
Updated:
Symbolic link had been created in
/etc/nginx/plesk.conf.d/vhosts in somedomain.xy.conf
#ATTENTION!
#
#DO NOT MODIFY THIS FILE BECAUSE IT WAS GENERATED AUTOMATICALLY,
#SO ALL YOUR CHANGES WILL BE LOST THE NEXT TIME THE FILE IS GENERATED.
server {
listen xx.xxx.xxx.x:443 ssl http2;
server_name somedomain.xy;
server_name www.somedomain.xy;
server_name ipv4.somedomain.xy;
ssl_certificate /opt/psa/var/certificates/scfPsMGvJ;
ssl_certificate_key /opt/psa/var/certificates/scfPsMGvJ;
ssl_client_certificate /opt/psa/var/certificates/scfSdpTzN;
client_max_body_size 128m;
root "/var/www/vhosts/somedomain.xy/httpdocs";
access_log "/var/www/vhosts/system/somedomain.xy/logs/proxy_access_ssl_log";
error_log "/var/www/vhosts/system/somedomain.xy/logs/proxy_error_log";
#extension letsencrypt begin
location /.well-known/acme-challenge/ {
root /var/www/vhosts/default/htdocs;
types { }
default_type text/plain;
satisfy any;
auth_basic off;
allow all;
location ~ ^/\.well-known/acme-challenge.*/\. {
deny all;
}
}
#extension letsencrypt end
location / {
proxy_pass https://xx.xxx.xxx.x:7081;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Accel-Internal /internal-nginx-static-location;
access_log off;
}
location /internal-nginx-static-location/ {
alias /var/www/vhosts/somedomain.xy/httpdocs/;
internal;
}
location ~ ^/(plesk-stat|awstats-icon|webstat|webstat-ssl|ftpstat|anon_ftpstat) {
proxy_pass https://xx.xxx.xxx.x:7081;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Accel-Internal /internal-nginx-static-location;
access_log off;
}
location ~ ^/proj_ci/ {
proxy_pass https://xx.xxx.xxx.x:7081;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Accel-Internal /internal-nginx-static-location;
access_log off;
}
location ~ "^/files/" {
proxy_pass https://xx.xxx.xxx.x:7081;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Accel-Internal /internal-nginx-static-location;
access_log off;
}
add_header X-Powered-By PleskLin;
}
server {
listen xx.xxx.xxx.x:80;
server_name somedomain.xy;
server_name www.somedomain.xy;
server_name ipv4.somedomain.xy;
client_max_body_size 128m;
root "/var/www/vhosts/somedomain.xy/httpdocs";
access_log "/var/www/vhosts/system/somedomain.xy/logs/proxy_access_log";
error_log "/var/www/vhosts/system/somedomain.xy/logs/proxy_error_log";
#extension letsencrypt begin
location /.well-known/acme-challenge/ {
root /var/www/vhosts/default/htdocs;
types { }
default_type text/plain;
satisfy any;
auth_basic off;
allow all;
location ~ ^/\.well-known/acme-challenge.*/\. {
deny all;
}
}
#extension letsencrypt end
location / {
proxy_pass http://xx.xxx.xxx.x:7080;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Accel-Internal /internal-nginx-static-location;
access_log off;
}
location /internal-nginx-static-location/ {
alias /var/www/vhosts/somedomain.xy/httpdocs/;
internal;
}
location ~ ^/(plesk-stat|awstats-icon|webstat|webstat-ssl|ftpstat|anon_ftpstat) {
proxy_pass http://xx.xxx.xxx.x:7080;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Accel-Internal /internal-nginx-static-location;
access_log off;
}
location ~ ^/proj_ci/ {
proxy_pass http://xx.xxx.xxx.x:7080;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Accel-Internal /internal-nginx-static-location;
access_log off;
}
location ~ "^/files/" {
proxy_pass http://xx.xxx.xxx.x:7080;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Accel-Internal /internal-nginx-static-location;
access_log off;
}
add_header X-Powered-By PleskLin;
}
I had tried to create in /etc/nginx/conf.d with the filename app_name.conf
map $http_upgrade $connection_upgrade {
default upgrade;
'' close;
}
upstream websocket {
server xx.xxx.xxx.x:8282;
}
server {
# listen xx.xxx.xxx.x:80;
# listen 443 default_server ssl;
listen 443 ssl http2;
server_name somedomain.xy;
location / {
proxy_pass http://xx.xxx.xxx.x:8282;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection $connection_upgrade;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
# proxy_read_timeout 120s;
proxy_read_timeout 86400;
# proxy_redirect default;
# proxy_redirect http://xx.xxx.xxx.x:8282/ /;
# proxy_redirect http://www.somedomain.xy/ /;
}
location /chat/ {
proxy_pass http://xx.xxx.xxx.x:8282;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection $connection_upgrade;
proxy_read_timeout 120s;
}
location /test {
rewrite ^/test(.*) $1 break;
proxy_pass http://127.0.0.1:8282;
}
location /wss {
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "Upgrade";
proxy_set_header Proxy "";
proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_pass http://xx.xxx.xxx.x:8282;
proxy_read_timeout 120s;
}
location /websocket {
proxy_pass http://xx.xxx.xxx.x:8282; ## WSPHP listening port
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_read_timeout 86400;
}
}
Also, In Nginx we are not able to see such directories they are /etc/nginx/sites-available/* and /etc/nginx/sites-enabled/* ,
we will be seeing under /etc/apache2
How to make stream_socket_get_name return the real IP address from the remote client?
$ip = stream_socket_get_name($socket, true);
The above returns something like 127.0.0.1:39872
nginx
server {
listen 8443 ssl;
server_name websocket.example.com;
ssl_certificate /var/ini/ssl/public.crt;
ssl_certificate_key /var/ini/ssl/private.key;
location / {
proxy_redirect off;
proxy_pass http://127.0.0.1:9000/;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_read_timeout 300;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-Proto https;
proxy_set_header X-Forwarded-For $remote_addr;
proxy_set_header X-Forwarded-Host $remote_addr;
}
}
The problem is stream_socket_get_name() operates on a file handle. In this case the file handle always connects to your proxy, so you can only get the proxy information. But you see those proxy_set_header directives? Those are how the remote IP is and remote port could be passed in. In your PHP, you'd have to check the values for the headers.