nginx配置示例-含反向代理

events {
    worker_connections  1024;
}
http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    server {
        listen 443 ssl http2;
        server_name teamemory.fun www.teamemory.fun;
        root /data/wwwroot/auctionPort;#项目路径
        charset utf-8;
        ssl_certificate    /data/3189862_www.teamemory.fun.pem;#.pem证书路径
        ssl_certificate_key  /data/3189862_www.teamemory.fun.key;#.key证书路径
        ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
        ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:HIGH:!aNULL:!MD5:!RC4:!DHE;
        ssl_prefer_server_ciphers on;
        ssl_session_cache shared:SSL:10m;
        ssl_session_timeout 10m;
        error_page 497  https://$host$request_uri;
        location / {
           include uwsgi_params;
           uwsgi_pass 127.0.0.1:8997;
           uwsgi_param UWSGI_SCRIPT auctionPort.wsgi;
           uwsgi_param UWSGI_CHDIR /data/wwwroot/auctionPort/;#项目路径
           
        }
        location /static/ {
        alias /data/wwwroot/auctionPort/static/; #静态资源路径
        }
        access_log  /data/wwwroot/auctionPort/www.teamemory.fun.log;
        error_log  /data/wwwroot/auctionPort/www.teamemory.fun.error.log;
    }
    server {
        listen 80;
        server_name teamemory.fun www.teamemory.fun;
        root /opt/zj;
    }
}

以上配置的80端口和443端口完全是两套内容。通过http:teamemory.fun  和 https://teamemory.fun 分别可以查看。

注意:谷歌浏览器默认会跳转到https的情况。

 

events {
    worker_connections  1024;
}
http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    server {
        listen 443 ssl http2;
        server_name teamemory.fun www.teamemory.fun;
        root /data/wwwroot/auctionPort;#项目路径
        charset utf-8;
        ssl_certificate    /data/3189862_www.teamemory.fun.pem;#.pem证书路径
        ssl_certificate_key  /data/3189862_www.teamemory.fun.key;#.key证书路径
        ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
        ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:HIGH:!aNULL:!MD5:!RC4:!DHE;
        ssl_prefer_server_ciphers on;
        ssl_session_cache shared:SSL:10m;
        ssl_session_timeout 10m;
        error_page 497  https://$host$request_uri;
        location / {
           include uwsgi_params;
           uwsgi_pass 127.0.0.1:8997;
           uwsgi_param UWSGI_SCRIPT auctionPort.wsgi;
           uwsgi_param UWSGI_CHDIR /data/wwwroot/auctionPort/;#项目路径
           
        }
        location /static/ {
        alias /data/wwwroot/auctionPort/static/; #静态资源路径
        }
        access_log  /data/wwwroot/auctionPort/www.teamemory.fun.log;
        error_log  /data/wwwroot/auctionPort/www.teamemory.fun.error.log;
    }
    server {
        listen 80;
        server_name teamemory.fun www.teamemory.fun;
        rewrite ^(.*) https://$server_name$1 permanent;  #代表重定向到443
    }

}

以上配置,无论你访问http:teamemory.fun  还是 https://teamemory.fun 均跳转到443对应的网页。

 

events {
    worker_connections  1024;
}
http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    server {
        listen 443 ssl http2;
        server_name teamemory.fun www.teamemory.fun;
        root /data/wwwroot/auctionPort;#项目路径
        charset utf-8;
        ssl_certificate    /data/3189862_www.teamemory.fun.pem;#.pem证书路径
        ssl_certificate_key  /data/3189862_www.teamemory.fun.key;#.key证书路径
        ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
        ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:HIGH:!aNULL:!MD5:!RC4:!DHE;
        ssl_prefer_server_ciphers on;
        ssl_session_cache shared:SSL:10m;
        ssl_session_timeout 10m;
        error_page 497  https://$host$request_uri;
        location / {
           include uwsgi_params;
           uwsgi_pass 127.0.0.1:8997;
           uwsgi_param UWSGI_SCRIPT auctionPort.wsgi;
           uwsgi_param UWSGI_CHDIR /data/wwwroot/auctionPort/;#项目路径
           
        }
        location /static/ {
        alias /data/wwwroot/auctionPort/static/; #静态资源路径
        }
        access_log  /data/wwwroot/auctionPort/www.teamemory.fun.log;
        error_log  /data/wwwroot/auctionPort/www.teamemory.fun.error.log;
    }
    server {
        listen 80;
        server_name teamemory.fun www.teamemory.fun;
        location / {
            proxy_pass http://localhost:7001;
        }
    }
}

以上是80端口反向代理到7001端口,相当于表面访问的是80端口,实际访问到了7001端口。

 

events {
    worker_connections  1024;
}
http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    server {
        listen 443 ssl http2;
        server_name teamemory.fun www.teamemory.fun;
        root /data/wwwroot/auctionPort;#项目路径
        charset utf-8;
        ssl_certificate    /data/3189862_www.teamemory.fun.pem;#.pem证书路径
        ssl_certificate_key  /data/3189862_www.teamemory.fun.key;#.key证书路径
        ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
        ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:HIGH:!aNULL:!MD5:!RC4:!DHE;
        ssl_prefer_server_ciphers on;
        ssl_session_cache shared:SSL:10m;
        ssl_session_timeout 10m;
        error_page 497  https://$host$request_uri;
        location / {
           include uwsgi_params;
           uwsgi_pass 127.0.0.1:8997;
           uwsgi_param UWSGI_SCRIPT auctionPort.wsgi;
           uwsgi_param UWSGI_CHDIR /data/wwwroot/auctionPort/;#项目路径
           
        }
        location /static/ {
        alias /data/wwwroot/auctionPort/static/; #静态资源路径
        }
        access_log  /data/wwwroot/auctionPort/www.teamemory.fun.log;
        error_log  /data/wwwroot/auctionPort/www.teamemory.fun.error.log;
    }
    server {
        listen 80;
        server_name teamemory.fun www.teamemory.fun;
        location / {  #匹配前端的地址
            root        /opt/zj/;
            try_files   $uri $uri/ /index.html last; //这里同时解决了vue的history模式,没有了 /#/
            index       index.html;
        }
        location /admin {  #匹配接口的地址
            proxy_pass http://localhost:7001;
        }
    }
}

 以上的配置,我们可以看出,前后端分离,前端后台的项目部署是分开的,利用nginx的反向代理来进行配置。

 

 

 

/usr/local/nginx/sbin/nginx -s reload   #nginx重启

 

posted @ 2020-07-01 09:48  茶记忆  阅读(338)  评论(0编辑  收藏  举报