Nginx安装使用:webserver及反向代理

试试nginx

centos7添加rpm源安装nginx

centos8直接yum安装即可

rpm -ivh http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm
yum repolist
yum install -y nginx

生成ssl的证书以及私钥

cd /etc/nginx/
openssl req -x509 -nodes -days 36500 -newkey rsa:2048 -keyout nginx.key -out nginx.crt

req是openssl证书请求的子命令
-newkey rsa:2048 -keyout private_key.pem 表示生成私钥(PKCS8格式)
-nodes 表示私钥不加密,若不带参数将提示输入密码
-x509表示输出证书
-days36500 为100年有效期,此后根据提示输入证书拥有者信息
-keyout 代表私钥全路径
-out 代表证书全路径

编辑nginx.conf

# vi /etc/nginx/nginx.conf
# For more information on configuration, see:
#   * Official English Documentation: http://nginx.org/en/docs/
#   * Official Russian Documentation: http://nginx.org/ru/docs/

user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;

# Load dynamic modules. See /usr/share/doc/nginx/README.dynamic.
include /usr/share/nginx/modules/*.conf;

events {
    worker_connections 1024;
}

http {
    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  /var/log/nginx/access.log  main;

    sendfile            on;
    tcp_nopush          on;
    tcp_nodelay         on;
    keepalive_timeout   65;
    types_hash_max_size 2048;

    include             /etc/nginx/mime.types;
    default_type        application/octet-stream;

    # Load modular configuration files from the /etc/nginx/conf.d directory.
    # See http://nginx.org/en/docs/ngx_core_module.html#include
    # for more information.
    include /etc/nginx/conf.d/*.conf;


    # github反代开始
    upstream github {
        ip_hash;
        server github.com:443;
    }

    server {
        listen       443 ssl;
        server_name  git.arloor.com;

        ssl_certificate      nginx.crt;
        ssl_certificate_key  nginx.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_pass https://github;
            proxy_set_header   Host             github.com:443;
        }

    }

    server {
        listen       80;
        server_name  git.arloor.com;
        # 返回301状态码,永久重定向
        rewrite ^(.*)$  https://$host$1 permanent;
    }
    # github反代结束

    # 默认80网站
    server {
        listen       80 default_server;
        listen       [::]:80 default_server;
        server_name  _;
        # 博客路径
        root         /usr/share/nginx/html;

        # Load configuration files for the default server block.
        include /etc/nginx/default.d/*.conf;

        location / {
        }

        # 上传文件的路径
        location /upload {
            root   html;
            index  index.html index.htm;
            proxy_pass http://upload/;
        }

        # 上传文件的路径
        location /uploadfile {
            root   html;
            index  index.html index.htm;
            proxy_pass http://upload/uploadfile;
        } 


        error_page 404 /404.html;
            location = /40x.html {
        }

        error_page 500 502 503 504 /50x.html;
            location = /50x.html {
        }
    }

    # 用于上传文件
    upstream upload {
        ip_hash;
        server localhost:8080;
    }

}

以上配置就完成了80、443端口到github的反向代理。

启动nginx

service nginx start