nginx提供网站首页的一个实例

如果既想匹配'/'进行反向代理,同时又想通过nginx提供网站首页,可以在server中进行如下配置:

 user  python; # 运行Nginx的用户
 worker_processes  auto; # 根据经验,一般为处理器核数的1-2倍
 error_log  logs/error.log  notice; #相对于/usr/local/nginx这个路径, debug info notice warn error crit几种级别
 pid   logs/nginx.pid;
 
 
 events {
      use epoll; # linux平台
   worker_connections  1024;
 }
 
 
 http {
     include       mime.types;
     default_type  application/octet-stream;
     log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                       '$status $body_bytes_sent "$http_referer" '
                       '"$http_user_agent" "$http_x_forwarded_for"';
     access_log  logs/access.log  main;
     sendfile        on;
     tcp_nopush     on;
     tcp_nodelay    on;
     keepalive_timeout  65;
 
     server {
         server_name  localhost www.xxx.top;
         listen       80;
         listen      443 ssl; # http和https均可以访问
         root /usr/local/nginx/html; # 全局定义默认根目录
         index index.html index.htm; # 全局定义默认首页地址
         ssl_certificate cert/server.crt;# 证书路径,相对/usr/local/nginx/conf路径
         ssl_certificate_key cert/server.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 utf-8;
         access_log  logs/host.access.log  main;
         error_log  logs/host.error.log  error;
 
        #静态文件访问
         location /static {
             # 注意使用alias,而非root,如果使用root,则变成了访问/home/python/static/static/下的静态文件
             alias /home/python/static;
             if ($query_string){
                 expires max;
             }
         }

        #网站图标
         location = /favicon.ico {
             access_log off;
             root /home/python/static/; # 网站图标位置/home/python/static/favicon.ico
         }
 
         location = / {
             index index.html index.htm;# 会重新访问/index.html
         }
 
         location = /index.html {
             root html;#html是相对于nginx安装目录的一个相对路径,会直接打开html/index.html,并返回该页面
         }
 
         location / {
             proxy_set_header HOST $host;
             proxy_set_header X-Forwarded-Proto $scheme;
             proxy_set_header X-Real-IP $remote_addr;
             proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
             proxy_pass http://127.0.0.1:8000;
         }
 
         #error_page  404              /404.html;
 
         # redirect server error pages to the static page /50x.html
         #
         error_page   500 502 503 504  /50x.html;
         location = /50x.html {
             root   html;
         }
}

 

posted @ 2021-03-26 16:28  eliwang  阅读(295)  评论(0)    收藏  举报