HTTP配置
user root;
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 1800s; #指定 KeepAlive 的超時時間(timeout)。指定每個 TCP 連接最多可以保持多長時間。Nginx 的默認(rèn)值是 75 秒,有些瀏覽器最多只保持 60 秒,所以可以設(shè)定為 60 秒。若將它設(shè)置為 0,就禁止了 keepalive 連接。
proxy_connect_timeout 1800s; #nginx跟后端服務(wù)器連接超時時間(代理連接超時)
proxy_send_timeout 1800s; #后端服務(wù)器數(shù)據(jù)回傳時間(代理發(fā)送超時)
proxy_read_timeout 1800s; #連接成功后,后端服務(wù)器響應(yīng)時間(代理接收超時)
fastcgi_connect_timeout 1800s; #指定nginx與后端fastcgi server連接超時時間
fastcgi_send_timeout 1800s; #指定nginx向后端傳送請求超時時間(指已完成兩次握手后向fastcgi傳送請求超時時間)
fastcgi_read_timeout 1800s; #指定nginx向后端傳送響應(yīng)超時時間(指已完成兩次握手后向fastcgi傳送響應(yīng)超時時間)
gzip on;
client_max_body_size 300m;
server {
listen 80;
server_name xxx.xxx.xxx.xxx;
charset utf-8;
#文件保存地址
location /xxxx{
alias /home/xxxx/uploadPath;
}
# 某某管理系統(tǒng)
location /xxxx/ {
proxy_pass http://127.0.0.1:6500;
}
# 前端文件存放位置
location /yyyyy {
alias /usr/local/nginx/html/yyyyy;
try_files $uri $uri/ /yyyyy/index.html;
index index.html;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
}
HTTPS環(huán)境搭建以及配置
1、驗證是否安裝ssl模塊
/usr/local/nginx/sbin/nginx -v
如果出現(xiàn) (configure arguments: --with-http_ssl_module), 則已安裝(下面的步驟可以跳過,直接進(jìn)行第3步)。
2、安裝ssl模塊
# 進(jìn)入到你的解壓縮后的nginx目錄,注意這里不是nginx安裝目錄,是解壓縮后的目錄
cd /usr/local/nginx-1.18.0
# 安裝模塊
./configure --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module
#切記不要執(zhí)行make install,否則會重新安裝nginx
make
#停止nginx服務(wù)
/usr/local/nginx/sbin/nginx -s stop
#替換之前的nginxcp /usr/local/nginx-1.18.0/objs/nginx /usr/local/nginx/sbin
#注意這里是大寫的V,小寫的只顯示版本號
/usr/local/nginx/sbin/nginx -V
#可以看到這里出現(xiàn)了configure arguments: --with-http_ssl_module 證明已經(jīng)安裝成功
#啟動Nginx
/usr/local/nginx/sbin/nginx
3、配置ssl證書
解壓縮下載好的證書(證書一般是pem文件和key文件,這里名字可以隨便改)
將下載好的證書上上傳到服務(wù)器,我將證書放在了root目錄下的card文件夾文章來源:http://www.zghlxwxcb.cn/news/detail-789821.html
# 創(chuàng)建存放證書路徑
mkdir /usr/local/nginx/card
# 配置nginx.comf文件
vim /usr/local/nginx/conf/nginx.conf
配置如下:文章來源地址http://www.zghlxwxcb.cn/news/detail-789821.html
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
server {
#監(jiān)聽443端口
listen 443;
#你的域名
server_name huiblog.top;
ssl on;
#ssl證書的pem文件路徑
ssl_certificate /usr/local/nginx/card/huiblog.top.pem;
#ssl證書的key文件路徑
ssl_certificate_key /usr/local/nginx/card/huiblog.top.key;
location / {
proxy_pass http://公網(wǎng)地址:項目端口號;
}
}
server {
listen 80;
server_name huiblog.top;
#將請求轉(zhuǎn)成https
rewrite ^(.*)$ https://$host$1 permanent;
}
}
4、重啟Nginx服務(wù)
/usr/local/nginx/sbin/nginx -s reload
Nginx 之 proxy_pass詳解
server {
listen 80;
server_name www.test.com;
# 情形A
# 訪問 http://www.test.com/testa/aaaa
# 后端的request_uri為: /testa/aaaa
location ^~ /testa/ {
proxy_pass http://127.0.0.1:8801;
}
# 情形B
# 訪問 http://www.test.com/testb/bbbb
# 后端的request_uri為: /bbbb
location ^~ /testb/ {
proxy_pass http://127.0.0.1:8801/;
}
# 情形C
# 下面這段location是正確的
location ~ /testc {
proxy_pass http://127.0.0.1:8801;
}
# 情形D
# 下面這段location是錯誤的
#
# nginx -t 時,會報如下錯誤:
#
# nginx: [emerg] "proxy_pass" cannot have URI part in location given by regular
# expression, or inside named location, or inside "if" statement, or inside
# "limit_except" block in /opt/app/nginx/conf/vhost/test.conf:17
#
# 當(dāng)location為正則表達(dá)式時,proxy_pass 不能包含URI部分。本例中包含了"/"
location ~ /testd {
proxy_pass http://127.0.0.1:8801/; # 記住,location為正則表達(dá)式時,不能這樣寫?。?!
}
# 情形E
# 訪問 http://www.test.com/ccc/bbbb
# 后端的request_uri為: /aaa/ccc/bbbb
location /ccc/ {
proxy_pass http://127.0.0.1:8801/aaa$request_uri;
}
# 情形F
# 訪問 http://www.test.com/namea/ddd
# 后端的request_uri為: /yongfu?namea=ddd
location /namea/ {
rewrite /namea/([^/]+) /yongfu?namea=$1 break;
proxy_pass http://127.0.0.1:8801;
}
# 情形G
# 訪問 http://www.test.com/nameb/eee
# 后端的request_uri為: /yongfu?nameb=eee
location /nameb/ {
rewrite /nameb/([^/]+) /yongfu?nameb=$1 break;
proxy_pass http://127.0.0.1:8801/;
}
access_log /data/logs/www/www.test.com.log;
}
server {
listen 8801;
server_name www.test.com;
root /data/www/test;
index index.php index.html;
rewrite ^(.*)$ /test.php?u=$1 last;
location ~ \.php$ {
try_files $uri =404;
fastcgi_pass unix:/tmp/php-cgi.sock;
fastcgi_index index.php;
include fastcgi.conf;
}
access_log /data/logs/www/www.test.com.8801.log;
}
到了這里,關(guān)于Linux中Nginx的HTTP和HTTPS常用配置以及proxy_pass詳解的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!