语法规则
location [=|~|~*|^~|!~|!~*] /uri/ { … }
模式 含义
location = /uri = 表示精确匹配
location ^~ /uri ^ 进行前缀匹配,~ 表示区分大小写
location ~ pattern ~ 区分大小写的匹配
location ~* pattern ~* 不区分大小写的匹配
location /uri 不带任何修饰符,也表示前缀匹配,但是在正则匹配之后
location / 通用匹配,任何未匹配到其它 location 的请求都会匹配到,相当于 switch 中的 default
location !~ 区分大小写不匹配
location !~* 不区分大小写不匹配
匹配优先级
首先精确匹配 =
其次前缀匹配 ^~
其次是按文件中顺序的正则匹配
然后匹配不带任何修饰的前缀匹配
最后是交给 / 通用匹配
当有匹配成功时候,停止匹配,按当前匹配规则处理请求
匹配的时候依照最佳匹配规则,按照能匹配到的最多的规则进行匹配
如 location ^~ /test/react/ 和 location ^~ /test/,请求 http://localhost/test/react/react.dev.js,会匹配 location /test/react/
1. nginx动态代理静态资源和接口
server {
listen 80;
server_name wetv.sdmc.tv;
index index.html;
// 代理静态资源
location / {
root /home/web; #静态资源目录
index index.html;
}
// 代理后台接口
location ~ ^/(maxis|xlhome|wetv) {
proxy_pass http://127.0.0.1:18083; #http://后面的要和upstream保持一致
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Scheme $scheme;
proxy_pass_header Server;
proxy_set_header Host $http_host;
proxy_redirect off;
}
# rewrite ^(.*)$ https://$host$1 permanent;
# proxy_pass http://127.0.0.1:18083;
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
2. ngix配置https域名
# HTTPS server
#
server {
listen 443 ssl;
server_name wetv.sdmc.tv;
ssl_certificate /usr/local/nginx/cert_key/wetv.sdmc.tv_bundle.crt;
ssl_certificate_key /usr/local/nginx/cert_key/wetv.sdmc.tv.key;
ssl_session_cache shared:SSL:1m;
ssl_session_timeout 5m;
ssl_ciphers HIGH:!aNULL:!MD5;
ssl_prefer_server_ciphers on;
location / {
root /home/web;
index index.html index.htm;
}
location ~ ^/(maxis|xlhome|wetv) {
proxy_pass http://wetv.sdmc.tv; #http://后面的要和upstream保持一致
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Scheme $scheme;
proxy_pass_header Server;
proxy_set_header Host $http_host;
proxy_redirect off;
}
}
3. nginx配置强制跳转https
//会存在post转为get的情况(不推荐使用)
rewrite ^(.*)$ https://$host$1 permanent;
// $request_uri表示后面的参数
return 307 https://xxx.xxx.com$request_uri;
/*$uri 和$request_uri说明*/
访问:http://192.168.128.137/test/
$uri:/test/test.html
$request_uri:/test/
4.nginx解决前端history模式访问报错的问题
location ^~/h5/tv/ais/tvPhoneConfirmIndex {
root /home/iot/web;
index index.html index.htm;
// 默认到这个地址下面去找
try_files $uri $uri/ /h5/tv/ais/tvPhoneConfirmIndex/index.html;
}
5.nginx 配置开机重启
vim /etc/init.d/nginx
#!/bin/sh
#
# nginx - this script starts and stops the nginx daemon
#
# chkconfig: - 85 15
# description: NGINX is an HTTP(S) server, HTTP(S) reverse \
# proxy and IMAP/POP3 proxy server
# processname: nginx
# config: /etc/nginx/nginx.conf
# config: /etc/sysconfig/nginx
# pidfile: /var/run/nginx.pid
# Source function library.
. /etc/rc.d/init.d/functions
# Source networking configuration.
. /etc/sysconfig/network
# Check that networking is up.
[ "$NETWORKING" = "no" ] && exit 0
nginx="/usr/local/nginx/sbin/nginx"
prog=$(basename $nginx)
NGINX_CONF_FILE="/usr/local/nginx/conf/nginx.conf"
[ -f /etc/sysconfig/nginx ] && . /etc/sysconfig/nginx
lockfile=/var/lock/subsys/nginx
make_dirs() {
# make required directories
user=`$nginx -V 2>&1 | grep "configure arguments:" | sed 's/[^*]*--user=\([^ ]*\).*/\1/g' -`
if [ -z "`grep $user /etc/passwd`" ]; then
useradd -M -s /bin/nologin $user
fi
options=`$nginx -V 2>&1 | grep 'configure arguments:'`
for opt in $options; do
if [ `echo $opt | grep '.*-temp-path'` ]; then
value=`echo $opt | cut -d "=" -f 2`
if [ ! -d "$value" ]; then
# echo "creating" $value
mkdir -p $value && chown -R $user $value
fi
fi
done
}
start() {
[ -x $nginx ] || exit 5
[ -f $NGINX_CONF_FILE ] || exit 6
make_dirs
echo -n $"Starting $prog: "
daemon $nginx -c $NGINX_CONF_FILE
retval=$?
echo
[ $retval -eq 0 ] && touch $lockfile
return $retval
}
stop() {
echo -n $"Stopping $prog: "
killproc $prog -QUIT
retval=$?
echo
[ $retval -eq 0 ] && rm -f $lockfile
return $retval
}
restart() {
configtest || return $?
stop
sleep 1
start
}
reload() {
configtest || return $?
echo -n $"Reloading $prog: "
killproc $nginx -HUP
RETVAL=$?
echo
}
force_reload() {
restart
}
configtest() {
$nginx -t -c $NGINX_CONF_FILE
}
rh_status() {
status $prog
}
rh_status_q() {
rh_status >/dev/null 2>&1
}
case "$1" in
start)
rh_status_q && exit 0
$1
;;
stop)
rh_status_q || exit 0
$1
;;
restart|configtest)
$1
;;
reload)
rh_status_q || exit 7
$1
;;
force-reload)
force_reload
;;
status)
rh_status
;;
condrestart|try-restart)
rh_status_q || exit 0
;;
*)
echo $"Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload|configtest}"
exit 2
esac
测试脚本是否正常运行
chmod a+x /etc/init.d/nginx
/etc/init.d/nginx start #启动
/etc/init.d/nginx stop #停止
配置开机自启
chkconfig --add /etc/init.d/nginx
chkconfig nginx on
nginx配置经验
最新推荐文章于 2023-11-05 22:43:38 发布