问题背景
在linux服务器中使用docker下载了Nginx,然后根据网上的教程来配置反向代理的时候发现80端口无法访问
server块的配置
server {
listen 80;
server_name 127.0.0.1 ;
#access_log /var/log/nginx/host.access.log main;
location / {
proxy_pass http://127.0.0.1:8080;
# 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 /usr/share/nginx/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;
#}
}
docker安装的Nginx中nginx.conf中没有server块,而是包含了一个conf.d文件夹中的所有文件我们用default.conf文件复制一个自己的conf文件。
这里和自带的default.conf不同的地方就是更改了location块
location / {
proxy_pass http://127.0.0.1:8080;
# index index.html index.htm;
}
此处配置结束,然后此时发现并不能访问。
解决方案
修改proxy_pass http://127.0.0.1:8080
由于我们是用docker安装的,所以此处Nginx中如果配置代理127.0.0.1的8080端口,其实是代理的docker容器中的服务器的8080端口,而不是我们的Linux服务器本来的8080端口,这里应该改成你服务器的真实ip
location / {
proxy_pass http://145.464.45.3:8080;
# index index.html index.htm;
}
将default备份并删除掉default.conf
由于我自己的conf文件和default.conf文件监听的都是80端口,所以只有一个能生效。
将default.conf备份成default.conf.bak
删除掉default.conf文件,此时我们自己的conf文件就生效了。
测试访问
成功访问。