nginx端口重定向及反向代理

Posted ColinLiu

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了nginx端口重定向及反向代理相关的知识,希望对你有一定的参考价值。

以下为是做Seafile的例子供参考,环境是宝塔nginx的配置文件:

 

直接上代码:

#监听80和8100端口,自动转向https
server {
    listen 80;
      listen 8100 ssl http2;
    server_name colin.com;
    #SSL-START SSL相关配置,请勿删除或修改下一行带注释的404规则
    #error_page 404/404.html;
    ssl_certificate    /www/server/panel/vhost/cert/colin.com/fullchain.pem;
    ssl_certificate_key    /www/server/panel/vhost/cert/colin.com/privkey.pem;
    ssl_protocols TLSv1.1 TLSv1.2 TLSv1.3;
    ssl_ciphers EECDH+CHACHA20:EECDH+CHACHA20-draft:EECDH+AES128:RSA+AES128:EECDH+AES256:RSA+AES256:EECDH+3DES:RSA+3DES:!MD5;
    ssl_prefer_server_ciphers on;
    ssl_session_cache shared:SSL:10m;
    ssl_session_timeout 10m;
    add_header Strict-Transport-Security "max-age=31536000";
    error_page 497  https://$host$request_uri;
    #SSL-END
    rewrite ^(.*)$ https://${server_name}$1 permanent; 
    }

#监控443端口,宝塔面板做重定向
server
{
    listen 443 ssl http2;
    server_name colin.com;
    index index.php index.html index.htm default.php default.htm default.html;
    root /home/seafile;
    
    #SSL-START SSL相关配置,请勿删除或修改下一行带注释的404规则
    #error_page 404/404.html;
    ssl_certificate    /www/server/panel/vhost/cert/colin.com/fullchain.pem;
    ssl_certificate_key    /www/server/panel/vhost/cert/colin.com/privkey.pem;
    ssl_protocols TLSv1.1 TLSv1.2 TLSv1.3;
    ssl_ciphers EECDH+CHACHA20:EECDH+CHACHA20-draft:EECDH+AES128:RSA+AES128:EECDH+AES256:RSA+AES256:EECDH+3DES:RSA+3DES:!MD5;
    ssl_prefer_server_ciphers on;
    ssl_session_cache shared:SSL:10m;
    ssl_session_timeout 10m;
    add_header Strict-Transport-Security "max-age=31536000";
    error_page 497  https://$host$request_uri;

    #SSL-END
    
    #ERROR-PAGE-START  错误页配置,可以注释、删除或修改
    #error_page 404 /404.html;
    #error_page 502 /502.html;
    #ERROR-PAGE-END
    
    #PHP-INFO-START  PHP引用配置,可以注释或修改
    #清理缓存规则

    location ~ /purge(/.*) {
        proxy_cache_purge cache_one $host$1$is_args$args;
        #access_log  /www/wwwlogs/colin.com_purge_cache.log;
    }
    
    #引用反向代理规则,注释后配置的反向代理将无效

    #PROXY-START/
    location ~* \\.(gif|png|jpg|css|js|woff|woff2)$
    {
    proxy_pass http://127.0.0.1:8000;
    proxy_set_header Host $host;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header REMOTE-HOST $remote_addr;
    expires 12h;
    }
    location /
    {
    proxy_pass http://127.0.0.1:8000;
    proxy_set_header Host $host;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header REMOTE-HOST $remote_addr;

    add_header X-Cache $upstream_cache_status;

    #Set Nginx Cache

    add_header Cache-Control no-cache;
    }

    # location ^~ /web-apps/{
    # rewrite /wap/(\\d+)/(.+) /$2?$args break;
    # proxy_pass https://127.0.0.1:8120;
    # }

    # location ^~ /welcome/{
    # rewrite /wap/(\\d+)/(.+) /$2?$args break;
    # proxy_pass https://127.0.0.1:8120;
    # }
    # location ^~ /6.3.0-111/{
    # rewrite /wap/(\\d+)/(.+) /$2?$args break;
    # proxy_pass https://127.0.0.1:8120;
    # }

    #PROXY-END/



    include enable-php-00.conf;
    #PHP-INFO-END
    
    #REWRITE-START URL重写规则引用,修改后将导致面板设置的伪静态规则失效
    include /www/server/panel/vhost/rewrite/colin.com.conf;
    #REWRITE-END
    
    #禁止访问的文件或目录
    location ~ ^/(\\.user.ini|\\.htaccess|\\.git|\\.svn|\\.project|LICENSE|README.md)
    {
        return 404;
    }
    
    #一键申请SSL证书验证目录相关设置
    location ~ \\.well-known{
        allow all;
    }
    
    access_log  /www/wwwlogs/colin.com.log;
    error_log  /www/wwwlogs/colin.com.error.log;
}

 

以上是关于nginx端口重定向及反向代理的主要内容,如果未能解决你的问题,请参考以下文章

[转帖]nginx 80端口重定向 转发到443端口

详解:Nginx反向代理及后端检测模块

串行端口重定向或拆分[关闭]

nginx反向代理功能及常用配置

Nginx安装及配置反向代理

Nginx 反向代理重定向到帐户/登录