在centeros7中配置nginx,http和https和tomcate

上一篇已经安装好了nginx

前提你有域名,服务器有外网IP,不然就是扯蛋

配置tomcate和绑定域名

找到nginx/conf/nginx.conf,做如下关键配置:


#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 erp {
server 127.0.0.1:8080;
}


    server {
        listen       80;
        server_name  erp.dgrxs.com;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            proxy_pass http://erp;
	    proxy_set_header Host $http_host;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        }

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

}

Tomcate修改

<Host name="www.hehe.com" appBase="webapps" unpackWARs="true" autoDeploy="true">

  <Context path="" docBase="../webapps/testweb" debug="0" reloadable="true"/> </Host>

https配置

运行 /usr/local/nginx/sbin/nginx -V ,如果没有 最后一行说明ssl没弄好

[root@61E6v9-server logs]# /usr/local/nginx/sbin/nginx -V
nginx version: nginx/1.15.5
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-28) (GCC) 
built with OpenSSL 1.0.2k-fips  26 Jan 2017
TLS SNI support enabled
configure arguments: --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module

1.2 Nginx开启SSL模块

切换到源码包:

1

cd /usr/nginx/nginx-1.11.3

 配置信息就应该这样写:

1

./configure --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module

./configure --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module --with-stream

 线上加入图片压缩功能

./configure --prefix=/usr/local/nginx \--with-http_stub_status_module \--with-http_ssl_module \--with-http_image_filter_module

make & make install

运行上面的命令即可,等配置完

配置完成后,运行命令 make

然后将刚刚编译好的nginx覆盖掉原有的nginx(这个时候nginx要停止状态

1

cp ./objs/nginx /usr/local/nginx/sbin/

然后启动nginx,仍可以通过命令查看是否已经加入成功

1

/usr/local/nginx/sbin/nginx -V 

在原本的基础上加上这个配置

server {
        listen 443 ssl;
        server_name www.dgrxs.com; #填写绑定证书的域名
        
        ssl_certificate	/usr/local/nginx/conf/crt/cert-151_www.dgrxs.com.crt;# 当前配置文件夹下建个目录存放证书
        ssl_certificate_key	/usr/local/nginx/conf/crt/cert-151_www.dgrxs.com.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;
        
		location / {
            proxy_pass http://www;
			proxy_set_header Host $http_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  $scheme; #防止重定向后变成http
        }
		
		error_page  404              /404.html;
		error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }

tomcate在原本基础上加2个地方

 <Connector port="8080" protocol="HTTP/1.1"
               connectionTimeout="20000"
               redirectPort="443"  proxyPrort="443" 
               disableUploadTimeout="false" />

在Engine中加上,原本默认的不要管,只管在Engine中加就可以了

<Valve className="org.apache.catalina.valves.RemoteIpValve" 
        protocolHeaderHttpsValue="https" remoteIpHeader="X-Forwarded-For" 
        protocolHeader="X-Forwarded-Proto" />
 

配置完,一定要重启nginx,不是reload,是kill再启动,和tomcate

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值