Nginx配置文件nginx.conf文件配置http,https請求

(一)nginx.conf文件配置示例:
#user  nobody;
worker_processes  1;

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/access.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"';


    sendfile        on;
    log_format  elkjson  '{ "@timestamp": "$time_iso8601", '
                                '"remote_addr": "$remote_addr", '
                                '"remote_user": "$remote_user", '
                                '"body_bytes_sent": "$body_bytes_sent", '
                                '"request_time": "$request_time", '
                                '"api_key": "$http_api_key", '
                                '"status": "$status", '
                                '"request": "$request", '
                                '"upsteam_re_time": "$upstream_response_time", '
                                '"request_method": "$request_method", '
                                '"http_referrer": "$http_referer", '
                                '"domain":"$server_name", '
                                '"body_bytes_sent": "$body_bytes_sent", '
                                '"upstream_addr": "$upstream_addr", ' 
                                '"http_x_forwarded_for": "$http_x_forwarded_for", '
                                '"http_user_agent": "$http_user_agent" }';

    access_log  logs/access.log  elkjson;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;
    

    # 配置服務端地址
    upstream grpclbs {
         server 179.19.3.4:8889 weight=5;
         server 179.19.3.3:8889 weight=5;
    }
   
    #gzip  on;

   # server {
      #  listen       80;
      #  server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

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

        #error_page  404              /404.html;

        # redirect server error pages to the static page /50x.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 4567 http2;
           location / {
              grpc_pass grpc://grpclbs;
           }
    }
    #http請求的方式;監聽本機端口8082,代理到172.19.3.71機器上的8084端口上去;【172.19.3.71機器上的nginx的listen爲8084,然後location中對應程序的端口】
    server {
        listen       8082;
        server_name  localhost;
        #include proxy_params表示獲包含外部配置proxy_params,記錄之前經過機器的IP地址等信息,詳情請看外面proxy_params文件】
        include  proxy_params;
        charset utf-8;
        location / {
            proxy_pass http://179.20.3.71:8084;
                   }
    }
    #這個是https請求的方式
    server {
        listen       4433;
        include  proxy_params;
        server_name  localhost;
        ssl on;
        ssl_certificate     server.crt;
        ssl_certificate_key  server.key;
        ssl_session_timeout  5m;
        ssl_protocols  TLSv1 TLSv1.1 TLSv1.2;
        ssl_ciphers 'ECDHE-S128-SHA256:ECDHE-ECDSA-AES128-SHA256:ECDHE-RSA-AES128-SHA:ECDHE-ECDSA-AES128-SHA:ECDHE-RSA-AES256-SHA384:ECDHE-ECDSA-AES256-SHA384:ECDHE-RSA-AES256-SHA:ECDHE-ECDSA-AES256-SHA:DHE-RSA-AES128-SHA256:DHE-RSA-AES128-SHA:DHE-DSS-AES128-SHA256:DHE-RSA-AES256-SHA256:DHE-DSS-AES256-SHA:DHE-RSA-AES256-SHA:AES128-GCM-SHA256:AES256-GCM-SHA384:AES128-SHA256:AES256-SHA256:AES128-SHA:AES256-SHA:AES:CAMELLIA:DES-CBC3-SHA:!aNULL:!eNULL:!EXPORT:!DES:!RC4:!MD5:!PSK:!aECDH:!EDH-DSS-DES-CBC3-SHA:!ED-RSA-DES-CBC3-SHA:!KRB5-DES-CBC3-SHA';
        ssl_prefer_server_ciphers   on;
        #ssl_dhparam      dhparams.pem;
        location / {
            include     proxy_params;
            proxy_pass http://179.20.3.71:8084;
        }
    }

    # 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;
    #    }
    #}

}

 

(二)proxy_params配置文件示列:

proxy_redirect off;

#經過的ip地址信息,保存在對應的請求頭中
proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Scheme $scheme;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

發佈了52 篇原創文章 · 獲贊 76 · 訪問量 14萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章