CentOS 上部署Nginx实现http301重定向https配置文件conf

本文涉及的产品
.cn 域名,1个 12个月
简介: CentOS 上部署Nginx实现http301重定向https配置文件conf

小知识,大挑战!本文正在参与“程序员必备小知识”创作活动。

cd /etc/nginx/conf.d
vim Testpage1.conf
server {
     listen 443 ssl; #配置HTTPS的默认访问端口号为443。此处如果未配置HTTPS的默认访问端口,可能会造成Nginx无法启动。Nginx 1.15.0以上版本请使用listen 443 ssl代替listen 443和ssl on。
     server_name www.certificatestests.com; #将www.certificatestests.com修改为您证书绑定的域名,例如:www.example.com。如果您购买的是通配符域名证书,要修改为通配符域名,例如:*.aliyun.com。
    ssl_certificate /home/wwwhttps/www.domain.com_nginx/www.domain.com.pem; #将domain name.pem替换成您证书的文件名称
    ssl_certificate_key /home/wwwhttps/www.domain.com_nginx/www.domain.com.key; #将domain name.key替换成您证书的密钥文件名称
    ssl_session_timeout 5m;
    ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4; #使用此加密套件
    ssl_protocols TLSv1 TLSv1.1 TLSv1.2; #使用该协议进行配置
    ssl_prefer_server_ciphers on;
    #charset koi8-r;
    access_log  /home/wwwlog/nginx/domain/b.access.log  main;
    location / {
        root   /usr/www/domain;    #站点目录
        index  index.php index.html index.htm;
        fastcgi_pass   unix:/run/php-fpm/www.sock;
        fastcgi_index  index.php;
        fastcgi_split_path_info  ^((?U).+\.php)(/?.+)$;
        fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
        fastcgi_param  PATH_INFO  $fastcgi_path_info;
        fastcgi_param  PATH_TRANSLATED  $document_root$fastcgi_path_info;
        include        fastcgi_params;
        if (!-e $request_filename) {
                rewrite  ^(.*)$  /index.php?s=/$1  last;
                break;
        }
    }
    error_page  404              /404.html;
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;
    }
    location ~ .*\.(gif|jpg|jpeg|png|bmp|swf|flv|ico)$ {
        root /usr/www/ygkj;
        expires 30d;
        access_log off;
    }
    location ~ .*\.(js|css|ttf)?$ {
        root /usr/www/ygkj;
        expires 7d;
        access_log off;
    }
}
server {
    #nignx同时监听80端口,并重定向到https
    listen 80;
    #填写绑定证书的域名
    server_name www.domain.com; 
    #把http的域名请求转成https
    return 301 https://$host$request_uri; 
}

systemctl restart nginx
server {
        listen 80;
        server_name myproxy.oa.com;
        #resolver 223.5.5.5; # 如果被代理的地址存在域名,需要加一个dns配置,否则会502,报错信息为:no resolver defined to resolve xxx.com
        index index.html;
        root /html;
        # 初始化变量
        set $node "";      # 后端主机地址
        set $proxy_url ""; # 后端页面地址
        set $proxy_scheme http; # 后端协议,默认http
        set $proxy_scheme_url ""; # 改造后的代理path,这里会带上协议约定,如 https-
        # 通过正则提取约定协议、后端节点和后端节点url
        if ( $request_uri ~* "^/(https-|)([^/]+)/(.*)$" ) {
            set $proxy_scheme_url "$1";
            set $node "$2";
            set $proxy_url "$3";
        }
        # 当协议变量值是https-的时候,设置代理后端协议为https,此规则就兼容了后端https的情况
        if ( $proxy_scheme_url = "https-" ) {
            set $proxy_scheme https;
        }
        # 不带后端地址直接访问代理IP的时候,定向到/html路径,里面可以放index.html导航页面,方便用户点击访问
        location ~ ^/($|static|favicon.ico) {
            break;
        }
        # 当访问的路径没有命中上述规则,且存在字符串的时候,将会进入到这个location开始反向代理
        location ~ ^/(.+)/ {
            # 使用 subs_filter 模块进行替换(不了解的话,请自行百度这个关键词)
            # ======================== replace Url begin ========================
            # 替换支持所有类型
            subs_filter_types *;
            # 替换html里面的静态资源引用,即 src=、href=等引用形式,另外还考虑到可能存在 / 打头的相对路径或“http://节点”的绝对路径引用形式。
            subs_filter "(href|src)(\s*)=(\s*)('|\")($proxy_scheme://$node|)/" '$1="http://$host/$proxy_scheme_url$node/' igr;
# 替换js里面的一些ajax请求地址:
            subs_filter "url:(\s*)('|\")/([^'\"]*)('|\")" "url: 'http://$host/$proxy_scheme_url$node/$3'" igr;
            # 这里替换实际访问发现还有问题的路径(这里主要是用了xmlhttprequest导致上述正则没命中)
            subs_filter "/workerProxy\?ip=" "http://$host/$proxy_scheme_url$node/workerProxy?ip=" igr;
            # you can write replace rule with subs_filter if found more.
            # ======================== replace Url end ===========================
            # 代理到动态后端
            proxy_pass $proxy_scheme://$node/$proxy_url;
            # 关闭gzip,以防替换不了内容(不能解决后端强制了gip压缩的情况...)
            proxy_set_header Accept-Encoding "";
            # 其他proxy参数略.
        }
}

server {
    #nignx同时监听80端口,并重定向到https
    listen 9001;
    #填写绑定证书的域名
    server_name 134.167.13.50;
    location / {
            root   /home/www/websockets;    #站点目录
            fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
            fastcgi_param  PATH_INFO  $fastcgi_path_info;
            fastcgi_param  PATH_TRANSLATED  $document_root$fastcgi_path_info;
            include        fastcgi_params;
        }
}


目录
相关文章
|
21天前
|
应用服务中间件 Linux nginx
让 CentOS 定时重启 Nginx
在CentOS上设置Nginx定时重启可通过`cron`或`systemctl`
|
22天前
|
Linux 虚拟化 数据安全/隐私保护
部署05-VMwareWorkstation中安装CentOS7 Linux操作系统, VMware部署CentOS系统第一步,下载Linux系统,/不要忘, CentOS -7-x86_64-DVD
部署05-VMwareWorkstation中安装CentOS7 Linux操作系统, VMware部署CentOS系统第一步,下载Linux系统,/不要忘, CentOS -7-x86_64-DVD
|
14天前
|
Ubuntu 应用服务中间件 Linux
Linux学习之Ubuntu 20中OpenResty的nginx目录里内容和配置文件
总的来说,OpenResty的Nginx配置文件是一个强大的工具,它允许你以非常灵活的方式定义你的Web服务的行为。
17 2
|
17天前
|
JavaScript 前端开发 应用服务中间件
Nginx——一个域名下部署多个Vue项目
如何在同一域名下部署第二个Vue项目而不影响现有项目:更新`vue.config.js`,设置`publicPath`为`/screen/`。修改Vue Router的`base`为`screen`。在Nginx配置中添加新location `/screen`,指向第二项目`dist`目录。测试访问`http://<域名>/screen/`。别忘了检查并修复任何遗漏的配置,如数据看板默认设置。
26 2
|
25天前
|
JavaScript 应用服务中间件 nginx
【项目部署系列教程】5. nginx配置反向代理,解决跨域接口的访问
【项目部署系列教程】5. nginx配置反向代理,解决跨域接口的访问
48 10
|
21天前
|
应用服务中间件 Linux nginx
CentOS 7 上配置 Nginx 作为反向代理
在CentOS 7上设置Nginx反向代理的步骤:安装Nginx;启动Nginx服务编辑`/etc/nginx/nginx.conf`;添加`proxy_pass http://app_server_address;将请求转发至应用服务器;重启Nginx`sudo systemctl restart nginx`;验证配置是否成功
|
21天前
|
应用服务中间件 Linux 网络安全
CentOS 7 上安装 Nginx
在 CentOS 7 上安装 Nginx 的步骤包括:添加 EPEL 仓库,安装 Nginx,启动 Nginx,配置防火墙规则,最后通过访问服务器 IP 验证安装是否成功
105 0
|
21天前
|
网络协议 应用服务中间件 nginx
Nginx的http块sendfile,keepalive_timeout的配置指令说明
Nginx的http块sendfile,keepalive_timeout的配置指令说明
|
23天前
|
前端开发 应用服务中间件 nginx
网页设计,若依项目修改(It must be done)01----若依打包位置,nginx代理前端静态资源和后端接口,就是怎样设置转载,访问固定端口,让他访问其他资料的配置文件,访问/,给你那些
网页设计,若依项目修改(It must be done)01----若依打包位置,nginx代理前端静态资源和后端接口,就是怎样设置转载,访问固定端口,让他访问其他资料的配置文件,访问/,给你那些
|
24天前
|
应用服务中间件 网络安全 nginx