1.基本配置與http配置
#user root root;#?設(shè)置nginx服務(wù)的系統(tǒng)使用用戶
worker_processes? auto;?#設(shè)置值和CPU核心數(shù)一致,?工作進(jìn)程數(shù)
error_log? /opt/nginxlogs/error.log;?#日志位置和日志級別
pid? ? ? ? logs/nginx.pid;?啟動(dòng)服務(wù)的pid
worker_rlimit_nofile 40960;
events {
? ? use epoll;
? ? worker_connections? 4096;?#每個(gè)進(jìn)程允許的最大連接數(shù)
}
##設(shè)定http服務(wù)器,利用它的反向代理功能提供負(fù)載均衡支持
http {
include? ? ? mime.types;#?設(shè)定mime類型,類型由mime.type文件定義
? ? default_type? application/octet-stream;
log_format? main? '$remote_addr - $http_x_forwarded_for - $remote_user [$time_local] "$request" ' #?以變量的形式設(shè)置日志格式,支持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指令設(shè)置了日志格式之后,需要用access_log指令指定日志文件的存放路徑
? ? server_tokens off;
? ? #access_log? on;
? ? sendfile? ? ? ? off;?#靜態(tài)資源訪問模式
? ? tcp_nopush? ? on;
keepalive_timeout? 25;#?連接超時(shí)時(shí)間
? ? 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;#?監(jiān)聽http端口58001
? ? ? ? server_name? localhost;?#監(jiān)聽的域名或者ip,例如地址欄輸入localhost,會被監(jiān)聽到進(jìn)入下一步
? ? ? ? client_max_body_size 50M;
? ? ? ? client_body_buffer_size 128k;
? ? ? ? index /default.jpg;
##?同級的location會依次匹配
#配置靜態(tài)資源訪問
? ? location /pics {
????? ? alias /data/ncdx/epg-data/cms-picture;#目標(biāo)路徑
? ? ? ? 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
觀察編譯時(shí)是否帶有“--with-http_ssl_module”
沒有需要卸載,重新編譯安裝
2.4生成證書
首先,進(jìn)入你想創(chuàng)建證書和私鑰的目錄,例如:
cd /usr/local/nginx/conf
創(chuàng)建服務(wù)器私鑰,命令會讓你輸入一個(gè)口令:
openssl genrsa -des3 -out server.key 1024
創(chuàng)建簽名請求的證書(CSR):
openssl req -new -key server.key -out server.csr
這里根據(jù)實(shí)際輸入,不輸入直接回車好像也沒影響
在加載SSL支持的Nginx并使用上述私鑰時(shí)除去必須的口令:
cp server.key server.key.org
openssl rsa -in server.key.org -out server.key
2.5配置nginx
最后標(biāo)記證書使用上述私鑰和CSR:
openssl x509 -req -days 365 -in server.csr -signkey server.key -out server.crt
修改Nginx配置文件,讓其包含新標(biāo)記的證書和私鑰:
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配置完成