nginx配置代理服务


nginx安教程后面再写,先写配置说明

  1. 一般拿到配置如下图,我们只需要修改server 部分.
#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;

load_module /root/nginx/modules/ngx_stream_module.so;

events {
    worker_connections  1024;
}

stream {
    upstream kube-apiserver {
        server 192.168.31.12:5901;
    }

    server {
        listen 15900;
        proxy_connect_timeout 2s;
        proxy_timeout 900s;
        proxy_pass kube-apiserver;
    }
}


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;
  1. 这里的server对象里面 ,listen意思是监听80端口.
  2. server_name是域名配置,多个域名空格隔开.
  3. rewrite是把http 重定向到https.
  4. location是主要的配置项.

4.1. proxy_pass 填写需要代理的本地服务,也可以是公网服务.

4.2. proxy_set_header当Host设置为$http_host时,则不改变请求头的值。

4.3. 其他代理头设置是为了获取访问请求的真实头部信息,并转发

server {
        listen 80;
        server_name www.xiaochun.cloud xiaochun.cloud; 
        rewrite ^(.*)$  https://$host$1 permanent;
        location / {
            proxy_pass http://127.0.0.1:8080;
            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_redirect default;
        }
    }

    server {
        listen 80;
        server_name api.xiaochun.cloud;
        rewrite ^(.*)$ https://$host$1 permanent;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            proxy_pass http://127.0.0.1:8000;
            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_redirect default;
        }

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


    # 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
  1. https服务配置基本与上方相同,多了https证书配置
  2. ssl_certificate ,购买的nginx SSL证书*.pem的绝对路径
  3. ssl_certificate_key ,购买的nginx SSL证书*.key的绝对路径
  4. 其他配置默认即可
server {
        listen       443 ssl;
        server_name api.xiaochun.cloud; 

        ssl_certificate "/opt/api.xiaochun/api.xiaochun.pem";
        ssl_certificate_key "/opt/api.xiaochun/api.xiaochun.key";

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

        ssl_ciphers  HIGH:!aNULL:!MD5;
        ssl_prefer_server_ciphers  on;
        location / {
            proxy_pass http://127.0.0.1:8000;
            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_redirect default;
        }

    }

    server {
        listen 443 ssl;
        server_name www.xiaochun.cloud xiaochun.cloud;

        ssl_certificate      /opt/xiaochun.cloud/xiaochun.pem;
        ssl_certificate_key  /opt/xiaochun.cloud/xiaochun.key;

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

        ssl_ciphers  HIGH:!aNULL:!MD5;
        ssl_prefer_server_ciphers  on;
        location / {
            proxy_pass http://127.0.0.1:8080;
            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_redirect default;
        }
    }
}