Nginx SSL

 

 

ip addr 查看ip

df -h 查看每个根路径的分区大小  du -sh * | sort -nr 当前目录磁盘使用情况  ll -h 查看目录下文件信息    chmod 777 xxx
less -r    find / -name '*.txt'   unzip filename.zip -d filepath  chmod 7777 s   zip -vr xmh.zip files/
ls -lt ls -t     tar -zcvf filename.tar.gz filename  端口占用netstat -nltp|grep 5001  解压 jar -xvf portal-app-1.0.0.jar

proxy_pass 带/替换 不带/追加

 

1,匹配的访问地址追加到代理地址后面
location /api1/ {
proxy_pass http://localhost:8080;
}
当访问 http://localhost/api1/xxx 时,会代理到 http://localhost:8080/api1/xxx

2,匹配的访问地址替换为代理地址
location /api2/ {
proxy_pass http://localhost:8080/; #只适合域名:端口的模式
}
当访问 http://localhost/api2/xxx 时,会代理到http://localhost:8080/xxx

3, 最好使用追加的方式
  location /xxl-job-admin {
    proxy_pass http://jnpf-xxjob-admin;
  }
http://xxxxx:30000/xxl-job-admin/jobinfo

 

    location /bd/ {
        proxy_pass http://xxxx:30182/business-process/;  #结尾有/进行替换 删除/db/
    }
http://xxxx:32489/bd/main-console/sso/token/ 可访问通 相当于跳转到 http://xxxx:30182/business-process/main-console/sso/token/

    location /business-process/ {
        proxy_pass http://xxxx:30182;   # 结尾没有/进行追加 只适合域名:端口的模式
    }
http://xxxx:32489/business-process/main-console/sso/token/ 可访问通

    location /main-console/ {
        proxy_pass http://xxxx:30182/business-process;   # 结尾没有/ 但是端口后面跟了路径,代理追加失败
    }
http://xxxx:32489/main-console/sso/token/ 报404

    location /pm {
        proxy_pass http://project-web;
    }
http://xxxx:32489/pm 结尾不加/  ng加上/后重定向回去  http://project-web/pm/

 

 

 跨域

    location /unify-message {
        add_header Access-Control-Allow-Origin *;
        index index.html index.htm;
        alias /usr/share/nginx/html;
        try_files $uri $uri/ /unify-message/index.html;
    }

    location /unify-message/xxx_gateway/ {
        add_header Access-Control-Allow-Origin *;
        add_header Access-Control-Allow-Methods 'GET, POST, OPTIONS';
        add_header Access-Control-Allow-Headers 'DNT,X-Mx-ReqToken,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Authorization';
        if ($request_method = ‘OPTIONS’) {
        return 204;
        }
        proxy_http_version 1.1;
        proxy_pass  http://${xxx_GATEWAY}/;
    }

 

   @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain filterChain) throws IOException, ServletException {

        HttpServletResponse res = (HttpServletResponse) response;
        res.setHeader("Access-Control-Allow-Origin", "*");
        res.setHeader("Access-Control-Allow-Methods", "*");
        res.setHeader("Access-Control-Max-Age", "3600");
        res.setHeader("Access-Control-Allow-Headers", "*");
        res.setHeader("Access-Control-Allow-Credentials", "true");

@Configuration
public class CorsConfig implements WebMvcConfigurer {
    @Override
    public void addCorsMappings(CorsRegistry registry) {
        // 设置允许跨域的路径
        registry.addMapping("/**")
                // 设置允许跨域请求的域名
                // 这里写的*表示允许所有域,实际情况可能有以下几种形式
                // .allowedOrigins("http://app.example.com:80"); 写全了,个人认为最正规的写法,但是更喜欢用下面这种
                // .allowedOrigins("http://app.example.com"); 只写了协议和域名,端口使用http默认的80,https的话是443,应该是最常用的写法(因为一般都使用默认的端口)
                // .allowedOrigins("http://123.123.123.123:8888"); 前端服务器没有域名的情况下,也可以使用ip地址
                .allowedOriginPatterns("*")
                // 是否允许cookie
                .allowCredentials(true)
                // 设置允许的请求方式
                .allowedMethods("GET", "POST", "DELETE", "PUT")
                // 设置允许的header属性
                .allowedHeaders("*")
                // 跨域允许时间,用于设置预检请求(OPTIONS方法)的缓存时间(单位秒)。设置为 3600(即1小时)表示浏览器可以缓存这个CORS响应信息1小时,期间对同一源的跨域请求不再发送预检请求,直接使用缓存结果,从而提高性能。
                .maxAge(3600);
    }
}

 

 

 

 

ng静态映射
user  root;
worker_processes  1;
error_log  logs/error.log;
error_log  logs/error.log  notice;
error_log  logs/error.log  info;
#pid        logs/nginx.pid;
events {
    worker_connections  1024;
}
http {
    include       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  logs/access.log  main;
    sendfile        on;
    #tcp_nopush     on;
    #keepalive_timeout  0;
    keepalive_timeout  65;
    client_max_body_size 100m;
    #gzip  on;
    server {
        listen       8000;
        server_name  localhost;
        location /xmh{
            alias /root/dz;
            index xmh.html;
            autoindex off;
            autoindex_exact_size off;
            autoindex_localtime off;
            add_header Access-Control-Allow-Origin *;
        }
     }
}
http://192.168.144.58:8000/xmh

 

关闭防火墙 systemctl stop firewalld.service

sudo sed -i -e "s|mirrorlist=|#mirrorlist=|g" /etc/yum.repos.d/CentOS-*
sudo sed -i -e "s|#baseurl=http://mirror.centos.org|baseurl=http://vault.centos.org|g" /etc/yum.repos.d/CentOS-*

sudo yum update
sudo yum install -y yum-utils
//一键安装上面四个依赖
yum -y install gcc zlib zlib-devel pcre-devel openssl openssl-devel
//先不安装 yum -y install make zlib zlib-devel gcc-c++ libtool openssl openssl-devel 

//创建一个文件夹
cd /usr/local
mkdir nginx
cd nginx
//下载tar包
wget http://nginx.org/download/nginx-1.13.7.tar.gz
tar -xvf nginx-1.13.7.tar.gz

//执行命令 考虑到后续安装ssl证书 添加两个模块
./configure 
进入src/os/unix/ngx_user.c ,注释一行代码
/*cd.current_salt[0] = ~salt[0];*/
去掉-Werror vim /usr/local/nginx/nginx-1.13.7/objs/Makefile
//执行make命令
make
//执行make install命令
make instal

启动 ./nginx -c nginx-1.13.7/conf/nginx.conf
重新启动 ./nginx -s reload

开放80端口:
firewall-cmd --zone=public --add-port=80/tcp --permanent
查询端口号80 是否开启:
firewall-cmd --query-port=80/tcp

  

 自签名证书

# 1. 首先创建SSL证书私钥,期间需要输入两次用户名和密码,生成文件为blog.key;
openssl genrsa -des3 -out blog.key 2048             密码 123456
# 2. 利用私钥生成一个不需要输入密码的密钥文件,生成文件为blog_nopass.key;
openssl rsa -in blog.key -out blog_nopass.key  
# 3. 创建SSL证书签名请求文件,生成SSL证书时需要使用到,生成文件为blog.csr;
# 在生成过程中,我们需要输入一些信息,需要注意的是Common Name需要和网站域名一致;
openssl req -new -key blog.key -out blog.csr    输入 11 12 13 14 15 localhost xmh
# 4. 生成SSL证书,有效期为365天,生成文件为blog.crt;
openssl x509 -req -days 365 -in blog.csr -signkey blog.key -out blog.crt
安装时 ./configure --prefix=/usr/local/nginx --with-http_ssl_module
/usr/local/nginx 解压后的文件放到该目录下,不要出现nginx-1.13.7目录,否则reload需要指定nginx.conf
cp ./objs/nginx /usr/local/nginx/nginx-1.13.7/sbin/
./nginx -c /usr/local/nginx/nginx-1.13.7/conf/nginx.conf
./nginx -s reload -c /usr/local/nginx/nginx-1.13.7/conf/nginx.conf
./nginx -s stop -c /usr/local/nginx/nginx-1.13.7/conf/nginx.conf
http://xxxxx:81/

server {
    listen       1671; # 同时支持HTTP
    listen       1672 ssl; # 添加HTTPS支持
    server_name  localhost;
  
    #SSL配置
    ssl_certificate      /usr/local/nginx/ssl/blog.crt; # 配置证书
    ssl_certificate_key  /usr/local/nginx/ssl/blog_nopass.key; # 配置证书私钥
    ssl_protocols        TLSv1 TLSv1.1 TLSv1.2; # 配置SSL协议版本
    ssl_ciphers          ECDHE-RSA-AES128-GCM-SHA256:HIGH:!aNULL:!MD5:!RC4:!DHE; # 配置SSL加密算法
    ssl_prefer_server_ciphers  on; # 优先采取服务器算法
    ssl_session_cache    shared:SSL:10m; # 配置共享会话缓存大小
    ssl_session_timeout  10m; # 配置会话超时时间
        
	location /{
            root   /usr/local/nginx/nginx-1.13.7/html;
            index  index.html index.htm;
        }
}

 

 

 

posted @ 2023-02-24 17:14  XUMT111  阅读(28)  评论(0)    收藏  举报