nginx

关注公众号 jb51net

关闭
首页 > 网站技巧 > 服务器 > nginx > nginx服务配置及重定向

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

总结

以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。

您可能感兴趣的文章:
阅读全文