部署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 使其修改生效。
 
 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM