nginx基本配置

最近因爲web項目多點部署,需要用到反向代理,所以選擇了現在和apache同樣火熱的nginx實踐了一下,非常龐大的一個工具,還有很多功能等待使用和研究,以下是初步使用的記錄過程:
起因:項目最開始使用的是DNS做負載,但是有一個緩存時間的問題,使用阿里雲服務器,最低也有10分鐘,無法達到高可用的要求。
一.安裝:
建議選擇編譯安裝的方式,可以添加其它模塊

1、下載新版本,到官網複製下載鏈接
wget http://nginx.org/download/nginx-1.15.10.tar.gz
2、解壓tar -zxvf nginx-1.15.10.tar.gz
3、編譯安裝
# 進入解壓目錄:
cd nginx-1.15.10/
# 配置並編譯安裝nginx:
 ./configure --prefix=/usr/local/nginx --with-http_ssl_module --with-stream --with-mail=dynamic
sudo make
sudo make install
# 啓動nginx:
sudo /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
#注意:-c 指定配置文件的路徑,不加的話,nginx會自動加載默認路徑的配置文件,可以通過 -h查看幫助命令。
# 查看nginx進程:
ps -ef|grep nginx


二.配置轉發,貼上nginx.conf代碼
1.nginx監聽的端口不能被佔用,http當然是80和https爲443
2.注意網上copy的代碼可能有空格,會導致配置文件錯誤
3.access_log logs/host.access.log;開啓日誌,可以查看訪問日誌和錯誤日誌
4. add_header backendIP $upstream_addr;
add_header backendCode $upstream_status;
增加實際訪問的ip和狀態,便於調試查看,實際運行可以隱藏

#user  nobody;
worker_processes  1;

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;


events {
    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;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    #gzip  on;
	
   upstream webserver {
	server xx.xxx.xx.xx:443 max_fails=2 fail_timeout=10s;
	server xxx.xx.xxx.xxx:443 max_fails=2 fail_timeout=10s;
   }		
   
   upstream backend.example.com {
	server 	xxx.com:443 max_fails=2 fail_timeout=10s;
	server  xxx.xxx.xx:443 max_fails=2 fail_timeout=10s;
   }

    server {
        listen       443;
        server_name xxxx.xxxxx.xx;
	
	ssl on;
	ssl_certificate /etc/ssl/xxx.pem;
	ssl_certificate_key /etc/ssl/xxxxx.key;
	ssl_session_timeout 5m;
	ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
	ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:HIGH:!aNULL:!MD5:!RC4:!DHE;
        ssl_prefer_server_ciphers on;


	
        #charset koi8-r;

        access_log  logs/host.access.log;

        location / {
		add_header backendIP $upstream_addr;
		add_header backendCode $upstream_status;
            proxy_pass   https://webserver;
            index  index.html index.htm;
        }
	
	
	location /eexce {
		add_header backendIP $upstream_addr;
		add_header backendCode $upstream_status;
		proxy_pass https://backend.example.com;
	}
        #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;
        }

        # proxy the PHP scripts to Apache listening on 127.0.0.1:80
        #
        #location ~ \.php$ {
        #    proxy_pass   http://127.0.0.1;
        #}

        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        #location ~ \.php$ {
        #    root           html;
        #    fastcgi_pass   127.0.0.1:9000;
        #    fastcgi_index  index.php;
        #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
        #    include        fastcgi_params;
        #}

        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        #location ~ /\.ht {
        #    deny  all;
        #}
    }
   # server {
   # 	listen 80;
   # 	server_name test.emeet.ai;
   # 	rewrite ^(.*)$ https://$host$1 permanent;
   # }

    # another virtual host using mix of IP-, name-, and port-based configuration
    #
    #server {
    #    listen       8000;
    #    listen       somename:8080;
    #    server_name  somename  alias  another.alias;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}


    # HTTPS server
    #
    #server {
    #    listen       443 ssl;
    #    server_name  localhost;

    #    ssl_certificate      cert.pem;
    #    ssl_certificate_key  cert.key;

    #    ssl_session_cache    shared:SSL:1m;
    #    ssl_session_timeout  5m;

    #    ssl_ciphers  HIGH:!aNULL:!MD5;
    #    ssl_prefer_server_ciphers  on;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}

}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章