Two sites on nginx but one getting all request - php

I have vps using nginx on that web server I got 2 conf (host1.com,host2.com) files on /etc/nginx/conf.d but 2 domain access to the same site. those site are on different technologies 1 PHP (Apache running on 88 ) and 1 python (gunicorn running on 5000) both site can be access from outside correctly using those ports.
site 1 conf
server{
listen 80;
root /var/www/host1.com/public;
index index.php index.html index.htm;
server_name host1.com;
location / {
try_files $uri $uri/ /index.php;
}
location ~ \.php$ {
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $remote_addr;
proxy_set_header Host $host;
proxy_pass http://127.0.0.1:88;
}
location ~ /\.ht {
deny all;
}
}
host2 conf
server {
listen 80;
server_name host2.com;
access_log /var/log/nginx/fundacion.log;
location / {
proxy_pass http://127.0.0.1:5000;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
Thanks in advance.

Restart nginx.
Clear your browser cache.

Related

Nginx proxy Apache2 php ERR_CONNECTION_RESET

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

Nginx Reverse Proxy To Remote Nginx Server Running PHP Application

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.

Nginx location is redirecting to main proxy_pass

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.

Wordpress stuck on "too many redirects"

I am trying to create a wordpress website that uses nginx as a reverse proxy to apache - apache serves only php requests.
I was able to successfully install wordpress but whenever I try to open the website it says "too many redirects". However when I try to open example.com/wp-admin it loads and I am able to login into and access the admin panel.
My /etc/nginx/sites-available/webproxy is:
server {
listen 80 default_server;
listen [::]:80 default_server;
root /var/www/html;
index index.php index.html index.htm;
server_name server_ip;
location / {
try_files $uri /index.php;
}
location ~ \.php$ {
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $remote_addr;
proxy_set_header Host $host;
proxy_pass http://127.0.0.1:8080;
}
location ~ /\.ht {
deny all;
}
}
I checked both apache and nginx error logs - empty.

Configure Nginx for work in Symfony 2

I try connected nginx and symfony 2. But my configure file not work for this framework.
server {
listen 80;
server_name test.com www.test.com;
access_log /var/log/nginx/test.access.log;
error_log /var/log/nginx/test.error.log;
location ~* \.(jpg|jpeg|gif|png|css|zip|tgz|gz|rar|bz2|doc|xls|exe|pdf|ppt|tar|wav|bmp|rtf|swf|ico|flv|txt|docx|xlsx)$ {
root /home/test/var/www/test.com/public_html/web;
index index.html index.php;
access_log off;
expires 30d;
}
location ~ /\.ht {
deny all;
}
location / {
proxy_pass http://127.0.0.1:8080/;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-for $remote_addr;
proxy_set_header Host $host;
proxy_connect_timeout 60;
proxy_send_timeout 90;
proxy_read_timeout 90;
proxy_redirect off;
proxy_set_header Connection close;
proxy_pass_header Content-Type;
proxy_pass_header Content-Disposition;
proxy_pass_header Content-Length;
}
}
For any static file which contain in directory
/home/test/var/www/test.com/public_html/web/css or
/home/test/var/www/test.com/public_html/web/js
return error 403
I try get static file use the url like this test.com/css/style.css
I resolve this problem. I forgot add user www-data ( the nginx process ) to group test ( the test is owner all files in your home dir ).

Categories