概述
日常工作中,在部署好nginx之後,針對配置文件,大家往往有以下困惑。
- 需要設置哪些參數?
- 參數值需要設置成多少?
- 有特定需求的,參數如何設置?如代理參數?ssl參數等。
下面分享一個配置文件一鍵生成神器,只要輸入域名,選擇相關選項,即可生成配置文件。
地址: https://nginxconfig.io
NGINX Config 支持 HTTP、HTTPS、PHP、Python、Node.js、WordPress、Drupal、緩存、逆向代理、日誌等各種配置選項。在線生成 Web 服務器 Nginx 配置文件。
操作配置也非常簡單,你需要做的只需要2步:
- 打開官方網站
- 按需求配置相關參數
系統就會自動生成特定的配置文件。生成的Nginx格式非常規範。
案例展示
案例描述
- 訪問example.com時,直接跳轉到https://example.com,完成http重定向。
- 訪問tomcat目錄時,直接跳轉到本機的8080端口。
生成的配置展示
1、生成主配置文件
cat /etc/nginx/sites-available/example.com.conf
server {
listen 443 ssl http2;
listen [::]:443 ssl http2;
server_name example.com;
root /var/www/example.com/var/www/abc.com;
# SSL
ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem;
ssl_trusted_certificate /etc/letsencrypt/live/example.com/chain.pem;
# security
include nginxconfig.io/security.conf;
# index.html fallback
location / {
try_files $uri $uri/ /index.html;
}
# reverse proxy
location /tomcat {
proxy_pass http://127.0.0.1:8080;
include nginxconfig.io/proxy.conf;
}
# additional config
include nginxconfig.io/general.conf;
}
# subdomains redirect
server {
listen 443 ssl http2;
listen [::]:443 ssl http2;
server_name *.example.com;
# SSL
ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem;
ssl_trusted_certificate /etc/letsencrypt/live/example.com/chain.pem;
return 301 https://example.com$request_uri;
}
# HTTP redirect
server {
listen 80;
listen [::]:80;
server_name .example.com;
include nginxconfig.io/letsencrypt.conf;
location / {
return 301 https://example.com$request_uri;
}
}
2、反向代理配置
cat /etc/nginx/nginxconfig.io/proxy.conf
proxy_http_version 1.1;
proxy_cache_bypass $http_upgrade;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
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 X-Forwarded-Proto $scheme;
proxy_set_header X-Forwarded-Host $host;
proxy_set_header X-Forwarded-Port $server_port;
3、其他nginx一般配置及安全配置
小結
1、通過上面的配置一個基礎的nginx模板就生成了,具體的其他需求細節大家可以根據自己項目的實際情況,進行調整。
2、重要的還是要熟悉每個參數代表的意義,這樣才能根據實際情況進行有針對性的調整,使用nginx性能達到最優。
閱讀更多 雪竹運維雜記 的文章