1.基本配置与http配置
#user root root;# 设置nginx服务的系统使用用户
worker_processes auto; #设置值和CPU核心数一致, 工作进程数
error_log /opt/nginxlogs/error.log; #日志位置和日志级别
pid logs/nginx.pid; 启动服务的pid
worker_rlimit_nofile 40960;
events {
use epoll;
worker_connections 4096; #每个进程允许的最大连接数
}
##设定http服务器,利用它的反向代理功能提供负载均衡支持
http {
include mime.types;# 设定mime类型,类型由mime.type文件定义
default_type application/octet-stream;
log_format main '$remote_addr - $http_x_forwarded_for - $remote_user [$time_local] "$request" ' # 以变量的形式设置日志格式,支持http请求变量
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" '
'upstream_response_time $upstream_response_time request_time $request_time ';
#access_log /opt/nginxlogs/access.log main buffer=2048k;
access_log /opt/nginxlogs/access.log main;
# 用了log_format指令设置了日志格式之后,需要用access_log指令指定日志文件的存放路径
server_tokens off;
#access_log on;
sendfile off; #静态资源访问模式
tcp_nopush on;
keepalive_timeout 25;# 连接超时时间
charset UTF-8;
client_header_timeout 10;
client_body_timeout 10;
reset_timedout_connection on;
send_timeout 10;
open_file_cache max=20480 inactive=60s;
open_file_cache_valid 30s;
open_file_cache_min_uses 2;
open_file_cache_errors on;
add_header X-Frame-Options SAMEORIGIN;
server {
listen 58001;# 监听http端口58001
server_name localhost; #监听的域名或者ip,例如地址栏输入localhost,会被监听到进入下一步
client_max_body_size 50M;
client_body_buffer_size 128k;
index /default.jpg;
## 同级的location会依次匹配
#配置静态资源访问
location /pics {
alias /data/ncdx/epg-data/cms-picture;#目标路径
proxy_redirect off;
proxy_store on;
proxy_store_access user:rw group:rw all:rw;
proxy_connect_timeout 10;
proxy_send_timeout 10;
proxy_read_timeout 10;
proxy_buffer_size 40k;
proxy_buffers 40 320k;
proxy_busy_buffers_size 640k;
proxy_temp_file_write_size 640k;
}
location /nginx_status {
stub_status on;
access_log off;
}
error_page 404 500 502 503 504 /default.jpg;
location = /50x.html {
root html;
}
#反向代理url
location /ucenter {
proxy_pass http://10.0.18.33:60000/ucenter/;
proxy_redirect off;
proxy_set_header Host $host:$server_port;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
}
2.nginx配置ssl证书部署https
2.1检查是否安装openssl
rpm –qa |grep openssl
观察是否安装openssl和openssl-devel包
2.2安装openssl
yum install openssl
yum install openssl-devel
2.3检查是否支持https_ssl_module
cd /usr/local/nginx/sbin
./nginx -V
观察编译时是否带有“--with-http_ssl_module”
没有需要卸载,重新编译安装
2.4生成证书
首先,进入你想创建证书和私钥的目录,例如:
cd /usr/local/nginx/conf
创建服务器私钥,命令会让你输入一个口令:
openssl genrsa -des3 -out server.key 1024
创建签名请求的证书(CSR):
openssl req -new -key server.key -out server.csr
这里根据实际输入,不输入直接回车好像也没影响
在加载SSL支持的Nginx并使用上述私钥时除去必须的口令:
cp server.key server.key.org
openssl rsa -in server.key.org -out server.key
2.5配置nginx
最后标记证书使用上述私钥和CSR:
openssl x509 -req -days 365 -in server.csr -signkey server.key -out server.crt
修改Nginx配置文件,让其包含新标记的证书和私钥:
server{
listen 90;
listen 443 ssl;
server_name 192.168.220.125;
ssl on;
ssl_certificate/usr/local/nginx/conf/server.crt;
ssl_certificate_key/usr/local/nginx/conf/server.key;
}
配置url
在https的server中增加location
location /unicom_ucenter {
proxy_pass http://10.0.18.33:60000/unicom_ucenter/;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
#proxy_set_header X-Forwarded-For $proxy_add_x_forworded_for;
}
重启nginx配置完成