nginx的服务配置及重定向问题

 更新时间:2024年06月18日 09:43:33   作者:小可爱(⑉°з°)-♡  

这篇文章主要介绍了nginx的服务配置及重定向问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教

目录
  • nginx的服务配置
  • 重定向
  • 总结

nginx的服务配置

vim /usr/local/lnmp/nginx/conf/nginx.conf
server {
        listen 80;
        server_name www.westos.org;

        location / {
                root /www1;
                index index.html;
        }
}
server {
        listen 80;
        server_name bbs.westos.org;

        location / {
                root /www2;
                index index.html;
        }
mkdir /www1
mkdir /www2
vim /www1/index.html
www.westos.org
vim /www2/index.html
bbs.westos.org

浏览器上验证:www.westos.org:输出/www1上的发布文件中的目录

bbs.westos.org:输出/www2上的发布文件中的目录

https端口:

server {
    listen       443 ssl;
    server_name  www.westos.org;

    ssl_certificate      cert.pem;
    ssl_certificate_key  cert.pem;

    ssl_session_cache    shared:SSL:1m;
    ssl_session_timeout  5m;

    ssl_ciphers  HIGH:!aNULL:!MD5;
    ssl_prefer_server_ciphers  on;

    location / {
        root   /www1;
        index  index.html index.htm;
    }
}
cd /etc/pki/tls/certs/
make cert.pem
Country Name (2 letter code) [XX]:cn
State or Province Name (full name) []:shaanxi
Locality Name (eg, city) [Default City]:xi'an
Organization Name (eg, company) [Default Company Ltd]:westos
Organizational Unit Name (eg, section) []:linux
Common Name (eg, your name or your server's hostname) []:server7
Email Address []:root@localhost

nginx -s reload
cp cert.pem /usr/local/lnmp/nginx/conf/
nginx -s reload

浏览器验证:https://www.westos.org

重定向

server {
        listen 80;
        server_name www.westos.org westos.org bbs.westos.org;

        rewrite ^(.*)$ https://www.westos.org permanent;
        }

        location / {
                root /www1;
                index index.html;
        }
}

主机上输入curl -I www.westos.org 重定向到https://www.westos.org

这种配置在访问网页下的具体页面时会弹出首页,显然这是不合理的

配置如下:

server {
        listen 80;
        server_name www.westos.org westos.org bbs.westos.org;

        rewrite ^(.*)$ https://www.westos.org$1 permanent;
        }

        location / {
                root /www1;
                index index.html;
        }
}

server {
        listen 80;
        server_name www.westos.org westos.org bbs.westos.org;
    rewrite ^/bbs$ http://bbs.westos.org permanent;
        rewrite ^/bbs/(.*)$ http://bbs.westos.org/$1 permanent;

        #rewrite ^(.*)$ https://www.westos.org$1 permanent;
        }

        #location / {
        #        root /www1;
        #        index index.html;
        #}
}
server {
       listen 80;
       server_name bbs.westos.org;

       location / {
               root /www2;
               index index.html;
       }
}

server {
        listen 80;
        server_name www.westos.org westos.org bbs.westos.org;
    #rewrite ^/bbs$ http://bbs.westos.org permanent;
        #rewrite ^/bbs/(.*)$ http://bbs.westos.org/$1 permanent;

        #rewrite ^(.*)$ https://www.westos.org$1 permanent;

    if ($host = "bbs.westos.org"){
        rewrite ^/(.*)$ http://www.westos.org/bbs/$1 permanent;

        }

        #location / {
        #        root /www1;
        #        index index.html;
        #}
}
#server {
#       listen 80;
#       server_name bbs.westos.org;
#
#       location / {
#               root /www2;
#               index index.html;
#       }
#}
}

总结

以上为个人经验,希望能给大家一个参考,也希望大家多多支持小闻网。

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。