docker安装nginx1.27.0

关于拉取不到镜像问题可以到这篇文章进行镜像配置

一、docker拉取nginx1.27.0镜像
docker pull nginx:1.27.0
二、创建映射容器的文件目录
mkdir -p -m 777 /mydata/nginx/conf/conf.d
mkdir -p -m 777 /mydata/nginx/log
mkdir -p -m 777 /mydata/nginx/html

三、创建文件default.conf放入/mydata/nginx/conf/conf.d

server {
    listen       80;
    listen  [::]:80;
    server_name  localhost;

    #access_log  /var/log/nginx/host.access.log  main;

    location / {
        root   /usr/share/nginx/html;
        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;
    #}
}

四、创建文件nginx.conf放入/mydata/nginx/conf


user  nginx;
worker_processes  auto;

error_log  /var/log/nginx/error.log notice;
pid        /var/run/nginx.pid;


events {
    worker_connections  1024;
}


http {
    include       /etc/nginx/mime.types;
    default_type  application/octet-stream;

    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    access_log  /var/log/nginx/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    keepalive_timeout  65;

    #gzip  on;

    include /etc/nginx/conf.d/*.conf;
}

五、创建文件50x.html放入/mydata/nginx/html

<!DOCTYPE html>
<html>
<head>
<title>Error</title>
<style>
html { color-scheme: light dark; }
body { width: 35em; margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif; }
</style>
</head>
<body>
<h1>An error occurred.</h1>
<p>Sorry, the page you are looking for is currently unavailable.<br/>
Please try again later.</p>
<p>If you are the system administrator of this resource then you should check
the error log for details.</p>
<p><em>Faithfully yours, nginx.</em></p>
</body>
</html>

六、创建文件index.html放入/mydata/nginx/html

<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
html { color-scheme: light dark; }
body { width: 35em; margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif; }
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>

<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>

<p><em>Thank you for using nginx.</em></p>
</body>
</html>

七、执行docker命令

如果你的前端不同项目想配置别的端口,需要一开始就在这里预设好 例如 -p 10010:10010

docker run -p 80:80 --name nginx \
--restart=always \
-v /mydata/nginx/conf/nginx.conf:/etc/nginx/conf/nginx.conf \
-v /mydata/nginx/conf/conf.d:/etc/nginx/conf.d \
-v /mydata/nginx/log:/var/log/nginx \
-v /mydata/nginx/html:/usr/share/nginx/html \
-d nginx:1.27.0

八、访问ip地址即可看到界面

注意:如果我们自己的项目需要进行配置,因为在nginx里采用的是include模式,可以直接在default.conf里进行修改即可,或者在default.conf同目录新增一个自己的配置,例如my.conf

 upstream backend{
        server 192.168.0.77:6060;
        keepalive 1000;
    }
 
 server {
    listen 10010;
    location / {
        root  /usr/share/nginx/html/systemDist;
        index index.html index.htm;
        try_files $uri $uri/ /index.html;
    }

    location /api {
        proxy_pass http://backend;
        rewrite ^/api/(.*)$ /$1 break;
    }
}

例如有个system的服务,我需要部署前端到nginx里,那么10010就是访问前端的端口,也对应前面docker run中配置的映射宿主机端口,127.0.0.77:6060为后端的请求地址,/api用来处理跨域问题做代理转发,意思是将访问带了/api的请求全部转发到192.168.0.77:6060中去,并且转发的过程中去掉了api前缀。

刷新nginx配置:

docker exec -it nginx nginx -s reload

### Nginx 出现 502 Bad Gateway 错误的原因分析 当遇到 Nginx 返回 `502 Bad Gateway` 错误时,通常意味着 Nginx 成功接收到了客户端请求并尝试将其转发给上游服务器(通常是应用服务器),但未能成功获取有效的响应。这可能是由于多种原因造成的。 #### 可能的原因及解决方案 #### 上游服务未运行或无法访问 如果配置文件中的 upstream 或 proxy_pass 指向的服务不可达,则会触发此错误。确保目标应用程序正在监听预期端口,并可通过网络访问[^1]。 ```bash # 测试连接到后端服务 telnet backend_server_ip port_number ``` #### 超时设置不当 有时,默认超时时间过短可能导致长时间处理的任务被中断而返回 502 状态码。适当调整相关参数可以解决问题: ```nginx http { ... fastcgi_read_timeout 300; proxy_connect_timeout 75s; proxy_send_timeout 300; proxy_read_timeout 300; } ``` #### 日志审查 查看 Nginx 的 error_log 文件有助于定位具体问题所在。日志路径一般位于 `/var/log/nginx/error.log` 中。通过分析这些记录能够更精准地判断故障根源[^2]。 ```bash tail -f /var/log/nginx/error.log ``` #### PHP-FPM 配置不匹配 对于基于 PHP 应用程序的情况,确认 php-fpm.conf 和 www.conf 设置合理非常重要。特别是 pm.max_children 参数应根据实际负载情况设定得当。 ```ini ; Adjust the number of child processes according to your server's capacity. pm.max_children = 10 ``` #### Docker 容器内部健康状况检查 考虑到当前环境涉及容器化部署,在宿主机上执行命令验证容器状态也是必要的步骤之一。利用 docker exec 进入容器内进一步排查可能存在的问题。 ```bash docker ps | grep nginx_container_name_or_id docker exec -it nginx_container_name_or_id bash ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值