部署web到nginx服务上

部署web项目到nginx服务器

// 这是一个NODE Web Server
// 我已经将这个服务的端口改成了8000端口
// 当nginx捕获到访问域名为xxx.xxx.com的时候
// 就会转发到本地的8000端口
server{
    server_name xxx.xxx.com; //域名
    listen 80;
    location / {
        # proxy_http_version 1.1;
        proxy_set_header Connection "";
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Host $http_host;
        proxy_set_header X-NginX-Proxy true;
        proxy_pass http://127.0.0.1:8000$request_uri;
        proxy_redirect off;
    }
}


// 当访问当前域名80端口的时候,会去访问 root设置的目录下的网页
// 如果服务是在8000端口, 就可以通过上面的配置将请求在上面域名80端口的请求转发到 服务器上面的8000端口 
// 通过这种转发的机制可以配置多个服务,如果需要使用https 只需要将80端口改为443
server { listen 80; server_name ceshi.banma.com; ------------》这里写入你自己的域名(www.ceshi1.com) index index.html index.htm index.php default.html default.htm default.php; root /var/data/www/banma; --------项目的目录 #error_page 404 /404.html; location / { index index.html index.php; if (-f $request_filename/index.html){ rewrite (.*) $1/index.html break; } if (-f $request_filename/index.php){ rewrite (.*) $1/index.php; } if (!-f $request_filename){ rewrite (.*) /index.php; } } location ~ [^/]\.php(/|$) { # comment try_files $uri =404; to enable pathinfo #try_files $uri =404; fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; include fastcgi_params; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; fastcgi_split_path_info ^(.+?\.php)(/.*)$; set $path_info $fastcgi_path_info; fastcgi_param PATH_INFO $path_info; try_files $fastcgi_script_name =404; #include fastcgi.conf; #include pathinfo.conf; } location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$ { expires 30d; } location ~ .*\.(js|css)?$ { expires 12h; } # Disallow access to .ht, .svn, .bzr, .git, .hg, .cvs directories location ~ /\.(ht|svn|bzr|git|hg|cvs) { deny all; } #access_log /date/nginx/bmp.com.conf/access.log main; }
  • 更多参考
    listen 80;        server_name www.aaa.con aaa.con;        location / {         proxy_pass http://www.aaa.con:8087/proj1/;        }
     location /proj1/ {
         proxy_pass   http://www.aaa.con:8087/proj1/;
    }
}
server {
  listen 80; 
  server_name www.bbb.con bbb.con; 
  location / { 
    proxy_pass http://www.aaa.con:8087/proj2/;
  }

  location /proj2/ {
    proxy_pass http://www.aaa.con:8087/proj2/;
  }
 }

   这段配置的意思是访问www.aaa.com或者aaa.com的请求,会被nginx映射到http://www.aaa.con:8087/proj1/;
而访问www.bbb.com或者bbb.com的请求,
会被映射到http://www.aaa.con:8087/proj2/。多配置location /proj1/的原因是避免CSS/JS等在html中设置了
项目名路径的资源因为nginx的反向代理造成丢失工程名而无法访问到资源。
这样,我们就能够在一台机器上发布针对若干个域名的WEB服务了。 server { listen 8000; server_name localhost; #charset koi8-r; #access_log logs/host.access.log main; location / { root /var/www; #index index.html index.htm; } location = /project1 { root /var/www/project1; try_files $uri $uri/ /project1/index.html; index index.html index.htm; } location = /project2{ root /var/www/project2; try_files $uri $uri/ /project2/index.html; index index.html index.htm; } }

https配置

  • 第一步申请证书
  • 第二步配置
server {
  listen    443 ssl;
  listen    [::]:443 ssl;
  server_name abc.com;
  root     /usr/share/nginx/html;
 
  ssl_certificate "/root/keys/abc.com.pem";
  ssl_certificate_key "/root/keys/abc.com.private.pem";
  include /etc/nginx/default.d/*.conf;
 
  location / {
  }
  error_page 404 /404.html;
    location = /40x.html {
  }
  error_page 500 502 503 504 /50x.html;
    location = /50x.html {
  }
}
 
server {
  listen    443 ssl;
  listen    [::]:443 ssl;
  server_name def.com;
  root     /usr/share/nginx/html;
 
  ssl_certificate "/root/keys/def.com.pem";
  ssl_certificate_key "/root/keys/def.com.private.pem";
  include /etc/nginx/default.d/*.conf;
 
  location / {
  }
  error_page 404 /404.html;
    location = /40x.html {
  }
  error_page 500 502 503 504 /50x.html;
    location = /50x.html {
  }
}
server {
         listen     80;
        server_name  x;
        rewrite ^(.*)$  https://$host$1 permanent; 
        }


  server {
        listen       443;
        server_name  x;
        ssl                  on;
        ssl_certificate      /etc/nginx/server.cer;
        ssl_certificate_key   /etc/nginx/server.key;
        ssl_session_timeout  5m;
        ssl_protocols TLSv1;
        ssl_ciphers  HIGH:!aNULL:!MD5;
        ssl_prefer_server_ciphers   on;

         location / {
              client_max_body_size    16m;
              client_body_buffer_size 128k;
              proxy_pass                          http://online/;
              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           X-Forwarded-Proto https;
              proxy_next_upstream   off;

              proxy_connect_timeout   30;
              proxy_read_timeout      300;
              proxy_send_timeout      300;
        }

    }

环境变量的配置

修改 /etc/profile 文件,在文件末尾加上如下两行代码 
PATH=$PATH:/usr/local/MATLAB/R2013a/bin 
export PATH

export PATH=$PATH:/usr/local/nginx/sbin

最后执行命令 source /etc/profile 或执行点命令 ./profile 使其修改生效。
 
 

部署web到nginx服务上

上一篇:移动端页面滚动到底部自动加载数据


下一篇:我用 Python 分析了一波热卖年货,原来大家都在买这些东西?