I am trying to connect to a proxypass in docker I have setup but I keep getting:
8#8: *1 api.example.local could not be resolved (3: Host not found)
I can access the proxy by going to http://api.example.com in my browser, but not if I go through the nginx proxy pass. My nginx is as follows, and please note that:
Have ipv6 disabled
I am resolving to 127.0.0.11
server {
listen 80;
server_name api.example.local;
resolver 127.0.0.11 ipv6=off;
location / {
root /code/api/public_html/;
index index.php index.html index.htm;
try_files $uri $uri/ /index.php?rt=$uri&$args;
}
location ~ \.php$ {
root /code/api/public_html/;
fastcgi_pass php:9000;
fastcgi_index index.php;
#fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
fastcgi_param PATH_INFO $fastcgi_path_info;
fastcgi_param ENV development;
fastcgi_param HTTPS off;
fastcgi_read_timeout 9600;
}
}
server {
index index.php index.html;
server_name www.example.local;
resolver 127.0.0.11 ipv6=off;
error_log /var/log/nginx/error.log;
access_log /var/log/nginx/access.log;
root /code/main/public_html;
location / {
root /code/main/public_html/;
index index.php index.html index.htm;
try_files $uri $uri/ /index.php?rt=$uri&$args;
}
location /api {
resolver 127.0.0.11 ipv6=off;
proxy_pass_header Set-Cookie;
proxy_pass_header P3P;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Fowarded-Host $host;
set $upstream api.example.local;
proxy_pass http://$upstream;
proxy_connect_timeout 60;
proxy_redirect off;
}
location ~ \.php$ {
root /code/main/public_html/;
fastcgi_pass php:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
fastcgi_param PATH_INFO $fastcgi_path_info;
fastcgi_param ENV development;
fastcgi_param HTTPS off;
fastcgi_read_timeout 300;
}
}
I also have the domains in my /etc/hosts. The OS is Ubuntu 18.04.
127.0.0.1 localhost
127.0.1.1 my-comp
127.0.0.1 www.example.local
127.0.0.1 api.example.local
What can I be doing wrong?
The nginx container and the upstream server need to be on the same docker network.
Example:
Assuming the nginx container is exposed ports 443 on the bridge, create a docker network called nginx-network and join it to that network.
Assuming also the upstream server is a container called ‘mysite’, join that container to the nginx-network, nginx will then be able to resolve the dns ‘mysite’.
Related
I am working on some docker containers, Where there is a "main" nginx host that then proxies to other nginx hosts
server {
listen 80;
server_name mcaq.me www.mcaq.me files.mcaq.me dev.mcaq.me priv.mcaq.me;
location / {
proxy_pass http://mcaq:8080;
proxy_redirect http://mcaq:8080/;
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;
}
}
This (above) is the main nginx container
server {
listen 8080;
index index.html index.php;
server_name mcaq.me www.mcaq.me;
root /code;
location ~ \.php$ {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass localhost:9000;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info;
}
}
This (above) is an example of one of the sub-nginx containers
The main nginx server is being controlled by certbot after running, so is actually listening on 443 ssl
This behaves as I expect, when connecting to https://mcaq.me/
Then, when going to https://mcaq.me/portfolio, it breaks
It shows the 8080 port, which I don't understand
If you instead do https://mcaq.me/portfolio/
It works as expected
I've tried different things like using proxy_redirect, but I am struggling to find a solution, or understand what the problem is
Any advice, or if you need more infomation, let me know. cheers!
My app files are located in phpfpm container and I need to serve them through nginx. I want to avoid mounting the same files in two containers, so I'm trying to figure out a way to serve them only from one, phpfpm, container. When I use reverse proxy to other containers:
server {
listen 0.0.0.0:8080;
server_name myapp.test;
location / {
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header HOST $http_host;
proxy_set_header X-NginX-Proxy true;
proxy_pass http://phpfpm:900;
proxy_redirect off;
}
}
I get 502 Bad Gateway error with the following error log record:
1 recv() failed (104: Connection reset by peer) while reading response header from upstream, client: 172.18.0.1, server: myapp.test, request: "GET / HTTP/1.1", upstream: "http://172.18.0.2:9000/", host: "myapp.test"
I guess it's because phpfpm container is not a HTTP server.
So, alternatively, I try using fastcgi_pass like so:
server {
listen 0.0.0.0:8080;
server_name myapp.test;
root /app;
location / {
try_files $uri $uri/index.php;
}
location ~ \.php$ {
fastcgi_pass phpfpm:9000;
fastcgi_index index.php;
include fastcgi.conf;
}
}
This serves *.php files as expected, but doesn't serve other files, namely static content.
How do I makenginx serve both .php and static files from my phpfpm container?
Here's my docker-compose.yml:
version: "3.7"
services:
phpfpm:
image: "php-fpm:7.3"
volumes:
- ./site:/app
ports:
- "9000:9000"
nginx:
image: "nginx:1.17"
volumes:
- ./nginx/app.conf:/opt/nginx/conf/nginx.conf
ports:
- "80:8080"
You have 2 issues:
You did not mount your static content into your Nginx container, therefore it cannot be served. Add this volume to your container
./site/public/:/var/www/html/public/:ro
You need to setup your Nginx config in order to serve this static content. You may try this one
server {
listen 0.0.0.0:8080;
server_name myapp.test;
root /var/www/html/public;
add_header X-Frame-Options "SAMEORIGIN";
add_header X-XSS-Protection "1; mode=block";
add_header X-Content-Type-Options "nosniff";
index index.html index.htm index.php;
charset utf-8;
location / {
try_files $uri /index.php?$query_string;
}
location = /favicon.ico { access_log off; log_not_found off; }
location = /robots.txt { access_log off; log_not_found off; }
location ~ \.php$ {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass phpfpm:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi.conf;
}
}
I'm fairly new to Nginx and web servers in general.
My setup is docker, Nginx, PHP/laravel, +let's encrypt.
I have this Nginx config file:
server {
listen 80;
server_name www.example.io;
return 301 https://example.io$request_uri;
}
server {
listen 80;
server_name example.io;
return 301 https://example.io$request_uri;
}
server {
listen 443 ssl;
server_name example.io;
ssl_certificate /etc/nginx/certs/example.io.pem;
ssl_certificate_key /etc/nginx/certs/example.io.key;
index index.php index.html;
error_log /var/log/nginx/error.log;
access_log /var/log/nginx/access.log;
root /var/www/public;
location ~ \.php$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass app:9000;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info;
}
location / {
try_files $uri $uri/ /index.php?$query_string;
gzip_static on;
}
}
I'm pretty sure my certificates are not what the problem is. because they do work when I have a single port 80 server and it has server_name example.io www.example.io but in that case, I'm unable to access the website through example.io as secure. while the www.example.io is secure. it also acts like two different websites. I believe the cookies in one do not implement in the other.
What I'm trying to achieve is, I wish to redirect both www.example.io and example.io to https://example.io
I'm using Docker for the first time, trying to build an NGINX proxy (also first time with NGINX). I've seen multiple guides that all seem to suggest I'm on the right path, but when I run the image, I get duplicate listen options for [::]:80 in /etc/nginx/conf.d/site.conf.
site.conf:
server {
listen 80;
listen [::]:80 ipv6only=on;
server_name gamersplane.local;
root /var/www;
index dispatch.php;
location / {
try_files $uri /dispatch.php$is_args$args;
}
location ~ \.php$ {
try_files $uri /dispatch.php =404;
fastcgi_pass api:9000;
fastcgi_index dispatch.php;
fastcgi_buffers 16 16k;
fastcgi_buffer_size 32k;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
#fixes timeouts
fastcgi_read_timeout 600;
include fastcgi_params;
}
}
api.conf:
server {
listen 80;
listen [::]:80 ipv6only=on;
server_name api.gamersplane.local;
root /var/www/api;
index dispatch.php;
location / {
try_files $uri /dispatch.php$is_args$args;
}
location ~ \.php$ {
try_files $uri /dispatch.php =404;
fastcgi_pass api:9000;
fastcgi_index dispatch.php;
fastcgi_buffers 16 16k;
fastcgi_buffer_size 32k;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
#fixes timeouts
fastcgi_read_timeout 600;
include fastcgi_params;
}
}
I have two compose files:
docker-compose.yal
proxy:
build:
context: ./nginx
dockerfile: Dockerfile
container_name: gamersplane-proxy
ports:
- 80:80
volumes:
- ../:/var/www
docker-compose.dev.yml
proxy:
volumes:
- ./nginx/dev/site.conf:/etc/nginx/conf.d/site.conf
- ./nginx/dev/api.conf:/etc/nginx/conf.d/api.conf
You need to remove ipv6_only=on, according to the documentation:
This parameter is turned on by default. It can only be set once on start.
So no need to add it to your config
The LNMP(linux nginx mysql php^_^) environment based on docker, all of which I am in a container.
At present, xdebug can be used normally.
But i need enter code herexdebug.remote_host = visitor IP and our IP is constantly changing.
This is an awkward situation.
And if i set xdebug.remote_connect_back = 1 to let xdebug handle the automatic return of IP.
Docker's container is started in the default bridge mode, so the IP obtained by PHP is the bridge IP.
That is: similar to 172.0.0.1, the IP address of the real client cannot be obtained, so xdebug cannot return correctly.
Xdebug.log logs also have records:
Log opened at 2018-07-29 07:39:59
I: Checking remote connect back address.
I: Checking header 'HTTP_X_FORWARDED_FOR'.
I: Remote address found, connecting to :9001.
W: Creating socket for ':9001', getaddrinfo: No such file or directory.
E: Could not connect to client. :-(
Log closed at 2018-07-29 07:39:59
This problem puzzled for a long time, still hope ,please not stint grant instruction! Thank you very much
This is my nginx host conf
server {
listen 80;
server_name test.lh.XXX.com;
access_log /var/log/nginx/access_XXX_log;
error_log /var/log/nginx/error_XXX_log;
index index.html index.htm index.php index.phtml;
client_max_body_size 100m;
set $public_root /home/xxx/www/xxx_xxx_www/public;
root $public_root;
if (!-e $request_filename) {
rewrite ^/(.*) /index.php?/$1 last;
}
location / {
try_files $uri $uri/ $uri=404;
}
location ~ \.php$ {
fastcgi_pass unix:/run/php/php7.0-fpm.sock;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
}
This is my nginx fastcgi_param
fastcgi_param QUERY_STRING $query_string;
fastcgi_param REQUEST_METHOD $request_method;
fastcgi_param CONTENT_TYPE $content_type;
fastcgi_param CONTENT_LENGTH $content_length;
fastcgi_param SCRIPT_NAME $fastcgi_script_name;
fastcgi_param REQUEST_URI $request_uri;
fastcgi_param DOCUMENT_URI $document_uri;
fastcgi_param DOCUMENT_ROOT $document_root;
fastcgi_param SERVER_PROTOCOL $server_protocol;
fastcgi_param REQUEST_SCHEME $scheme;
fastcgi_param HTTPS $https if_not_empty;
fastcgi_param GATEWAY_INTERFACE CGI/1.1;
fastcgi_param SERVER_SOFTWARE nginx/$nginx_version;
fastcgi_param REMOTE_ADDR $remote_addr;
fastcgi_param REMOTE_PORT $remote_port;
fastcgi_param SERVER_ADDR $server_addr;
fastcgi_param SERVER_PORT $server_port;
fastcgi_param SERVER_NAME $server_name;
# PHP only, required if PHP was built with --enable-force-cgi-redirect
fastcgi_param REDIRECT_STATUS 200;