1. 模塊說明
1.1 全局塊:配置影響nginx全局的指令。一般有運行nginx服務器的用戶組,nginx進程pid存放路徑,日誌存放路徑,配置文件引入,允許生成worker process數等。
1.2 events塊:配置影響nginx服務器或與用戶的網絡連接。有每個進程的最大連接數,選取哪種事件驅動模型處理連接請求,是否允許同時接受多個網路連接,開啓多個網絡連接序列化等。
1.3 http塊:可以嵌套多個server,配置代理,緩存,日誌定義等絕大多數功能和第三方模塊的配置。如文件引入,mime-type定義,日誌自定義,是否使用sendfile傳輸文件,連接超時時間,單連接請求數等。
1.4 server塊:配置虛擬主機的相關參數,一個http中可以有多個server。
1.5 location塊:配置請求的路由,以及各種頁面的處理情況。
2. nginx.conf配置
#配置用戶或者組,默認爲nobody nobody
user root;
#允許生成的進程數,默認爲1
worker_processes auto;
#制定日誌路徑,級別。這個設置可以放入全局塊,http塊,server塊,級別以此爲:debug|info|notice|warn|error|crit|alert|emerg
error_log /var/log/nginx/error.log;
#指定nginx進程運行文件存放地址
pid /run/nginx.pid;
# Load dynamic modules. See /usr/share/nginx/README.dynamic.
#include /usr/share/nginx/modules/*.conf;
events {
#最大連接數,默認爲512
worker_connections 1024;
}
http {
#文件擴展名與文件類型映射表
include mime.types;
#默認文件類型,默認爲text/plain
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;
access_log /var/log/nginx/access.log main;
#允許sendfile方式傳輸文件,默認爲off,可以在http塊,server塊,location塊。
sendfile on;
#tcp_nopush on;
#keepalive_timeout 0;
#連接超時時間,默認爲75s,可以在http,server,location塊。
keepalive_timeout 65;
#開啓gzip
gzip on;
include /etc/nginx/conf.d/*.conf;
}
3. 基於nginx.conf配置https(如ngin.conf中的配置路徑include)
server {
listen 80;
server_name youhost;
access_log logs/access.log;
rewrite ^(.*)$ https://$host$1 permanent;
}
server {
listen 443 ssl;
server_name youhost;
access_log logs/access.log;
ssl_certificate /etc/nginx/ssl/youhost/you.pem;
ssl_certificate_key /etc/nginx/ssl/youhost/you.key;
ssl_session_timeout 10m;
ssl_prefer_server_ciphers on;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_ciphers AESGCM:ALL:!DH:!EXPORT:!RC4:+HIGH:!MEDIUM:!LOW:!aNULL:!eNULL;
location /
{
proxy_next_upstream error timeout invalid_header http_500 http_502 http_503 http_504 http_404;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Cookie "$http_cookie";
proxy_hide_header X-Powered-By;
proxy_hide_header X-AspNet-Version;
proxy_hide_header X-AspNetMvc-Version;
proxy_pass http://127.0.0.1:8088;
}
#靜態資源、圖片等
location /img/case {
alias /opt/talent/case/;
autoindex on;
}
}