Janrs.com | 杨建勇
Janrs.com | 杨建勇

Dcoker部署nginx

[!Tip]
Docker部署Nginx服务

转载请注明出处:https://janrs.com


Docker部署nginx服务

[!NOTE]
镜像使用的是阿里的私人镜像
安装目录为/docker/nginx-web

1.登录阿里镜像


docker login --username=yjy86868@163.com registry.cn-shenzhen.aliyuncs.com

2.创建docker网络

[!NOTE]
因为要使用到redis以及php,所以网络
我这边使用的自定义的bridge网络

docker network create web-net

3.部署nginx

3.1 创建目录

mkdir -p /docker/nginx-web

3.2 创建nginx.conf

创建

cd /docker/nginx-web && vim 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;
}

3.3 创建default.conf

创建

cd /docker/nginx-web && mkdir conf.d && cd conf.d && vim default.conf

添加以下默认配置

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

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

    root /usr/share/nginx/html/;

    location / {
        index  index.html index.htm index.php;
    }

    #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:80;
    #}

    # deny access to .htaccess files, if Apache's document root
    # concurs with nginx's one
    #
    #location ~ /\.ht {
    #    deny  all;
    #}
}

3.4 支持php解析的conf模板

[!NOTE]
我的php项目都放在/php_projects目录下
web项目目录/www不一样

server {
    listen       80;
    listen  [::]:80;
    server_name    local-laravel-rbac-api.com;

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

    location / {
        if (!-e $request_filename){
            rewrite  ^(.*)$  /index.php?s=$1  last;   break;
        }
    }

    #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:80;
    #}

    # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
    #
    location ~ \.php$ {
        root /php_projects/laravel-rbac-api/public/;
        fastcgi_pass   php81-fpm:9000;
        fastcgi_param  SCRIPT_FILENAME  $document_root$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;
    #}
}

3.5 部署服务

docker run --name nginx-web \
-p 80:80 \
-v /docker/nginx-web/conf.d/:/etc/nginx/conf.d/ \
-v /docker/nginx-web/conf.d/default.conf/:/etc/nginx/conf.d/default.conf \
-v /docker/nginx-web/nginx.conf/:/etc/nginx/nginx.conf \
-v /docker/nginx-web/log/:/var/log/nginx \
-v /www/:/usr/share/nginx/html/ \
--network web-net \
-d registry.cn-shenzhen.aliyuncs.com/yjy_k8s/nginx:v1.23.1
如果你有任何问题,欢迎在底部留言。或者点击加入微信技术交流群 | 我的GitHub

码仔

文章作者

Janrs.com

发表回复

textsms
account_circle
email

Janrs.com | 杨建勇

Dcoker部署nginx
[!Tip] Docker部署Nginx服务 转载请注明出处:https://janrs.com Docker部署nginx服务 [!NOTE] 镜像使用的是阿里的私人镜像 安装目录为/docker/nginx-web 1.登录阿里镜像 docker log…
扫描二维码继续阅读
2022-02-20