记录:阿里云服务器网站搭建(4)

ops/2024/9/23 11:16:21/

Docker安装Nginx

现阶段主要目的是做一些静态资源路径的转发代理,相当于一个web服务器,tomcat也可以设置凡访问静态资源。但考虑到后续还需要作为代理服务器对域名等进行代理转发,所以使用nginx。

准备好要挂载的nginx配置目录

mkdir -p /mydata/nginx/conf

启动临时nginx容器用于拷贝文件

docker run -p 80:80 --name nginx -d nginx

拷贝出 Nginx 容器的配置

# 将nginx容器中的nginx目录复制到本机的/mydata/nginx/conf目录
docker container cp nginx:/etc/nginx /mydata/nginx/conf# 复制的是nginx目录,将该目录的所有文件移动到 conf 目录
mv /mydata/nginx/conf/nginx/* /mydata/nginx/conf/# 删除多余的 /mydata/nginx/conf/nginx目录
rm -rf /mydata/nginx/conf/nginx

删除临时容器

docker rm nginx -f

启动nginx容器

docker run -p 80:80 --name nginx \
-v /mydata/nginx/html:/usr/share/nginx/html \
-v /mydata/nginx/logs:/var/log/nginx \
-v /mydata/nginx/conf/:/etc/nginx \
-d nginx

设置自启动

docker update nginx --restart=always

测试nginx

echo '<h1>nginx sucess</a></h1>' \
>/mydata/nginx/html/index.html

访问http://服务器ip。如下表示nginx成功。

Nginx配置

nginx配置文件介绍(网图)

配置文件/mydata/nginx/conf/nginx.conf  

可以看到,在 http 块中最后有 include /etc/nginx/conf.d/*.conf; 这句配置说明在 conf.d 目录下所有 .conf 后缀的文件内容都会作为 nginx 配置文件 http 块中的配置。

这是为了防止主配置文件太复杂,也可以对不同的配置进行分类。


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

我们可以在/mydata/nginx/conf/conf.d/下新建自定义的配置文件,具体可以参考default.conf

nginx配置反向代理 80端口到tomcat8080端口

server {listen       80;listen  [::]:80;server_name  121.41.225.xxx;  #域名或者IP地址;#access_log  /var/log/nginx/host.access.log  main;location / {root   /usr/share/nginx/html;index  index.html index.htm;proxy_pass http://121.41.225.xxx:8080/; #当你访问80端口可以实现向8080端口转发}#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访问静态资源配置

静态资源都放在nginx/html/static下

设置 /static/ 下所有的请求都转给 nginx
location /static/ {
    root /user/share/nginx/html;
}

访问资源测试,成功:


http://www.ppmy.cn/ops/11498.html

相关文章

TS学习3-枚举

目录 1&#xff0c;枚举2&#xff0c;定义枚举3&#xff0c;枚举的规则1&#xff0c;枚举的值可以是字符串或数字&#xff0c;分别称为字符串枚举、数字枚举2&#xff0c;数字枚举的值会自增3&#xff0c;枚举会出现在编译结果中&#xff0c;并且数字枚举和字符串枚举&#xff…

淘宝扭蛋机小程序开发:开启幸运与惊喜的新篇章

在移动互联网时代&#xff0c;小程序以其便捷、快速和轻量级的特点&#xff0c;正迅速成为用户日常生活中的重要一环。为了满足广大用户对新鲜、有趣体验的追求&#xff0c;淘宝扭蛋机小程序应运而生&#xff0c;为用户带来一场充满惊喜和乐趣的幸运之旅。 淘宝扭蛋机小程序的…

Collections.singletonList

1、Collections.singletonList public static <T> List<T> singletonList(T o) {return new SingletonList<>(o); } 列表只有一个元素&#xff0c;节省内存&#xff0c;返回列表不可以改变。 2、Arrays.asList public static <T> List<T> asL…

MongoDB学习【一】MongoDB简介和部署

MongoDB简介 MongoDB是一种开源的、面向文档的、分布式的NoSQL数据库系统&#xff0c;由C语言编写而成。它的设计目标是为了适应现代Web应用和大数据处理场景的需求&#xff0c;提供高可用性、横向扩展能力和灵活的数据模型。 主要特点&#xff1a; 文档模型&#xff1a; Mon…

十几个好用的学习以及AI网站

目录 1.识典古籍 2.华文慕课 3.历代人物 4.北大出版社电子书架 5.WaytoAGI 6.W3Schools 7.AI帮个忙 8.InsCode 9.文心一格 10.即使设计 11.AI绘画 12.无界AI 13.Midjourney中文站 14.其它 1.识典古籍 地址&#xff1a;识典古籍-古籍在线阅读平台 “识典古籍”是…

C语言指针+-整数、指针-指针、指针关系运算、指针和数组、二级指针、指针数组

文章目录 前言一、指针 - 整数二、指针 - 指针三、指针的关系运算四、指针和数组五、二级指针六、指针数组指针数组可以将几个一维数组模拟成二维数组 总结 前言 C语言指针整数、指针-指针、指针关系运算、指针和数组、二级指针、指针数组等介绍&#xff0c;还包括指针数组将几…

k8s的资源对象Deployment该如何使用?

k8s的资源对象Deployment该如何使用&#xff1f; Kubernetes&#xff08;k8s&#xff09;是一个开源的容器编排系统&#xff0c;用于自动化应用程序部署、扩展和管理。在k8s中&#xff0c;Deployment是管理Pod副本的一种方式&#xff0c;它确保了指定数量的Pod副本始终运行。本…

kubernetes安装ingress-nginx

下载安装文件 首先&#xff0c;需要匹配Ingress-nginx版本和kubernetes版本。 在https://github.com/kubernetes/ingress-nginx可以找到&#xff0c;如下图所示&#xff1a; 这里一定要选择kubernetes对应的ingress-nginx版本 要不会报一些奇怪的错误&#xff01; 博主k8s版本…