nginx 学习篇:location 模块和反向代理配置
一、概述 location 模块是 nginx 中用的最多的,也是最重要的模块了,什么负载均衡啊、反向代理啊、虚拟域名啊都与它相关。 location 根据它字面意思就知道是来定位的,定位 URL,解析 URL,所以,它也提供了强大的正则匹配功能,也支持条件判断匹配,用户可以通过 location 指令实现 Ngi ... 阅读更多
一、概述 location 模块是 nginx 中用的最多的,也是最重要的模块了,什么负载均衡啊、反向代理啊、虚拟域名啊都与它相关。 location 根据它字面意思就知道是来定位的,定位 URL,解析 URL,所以,它也提供了强大的正则匹配功能,也支持条件判断匹配,用户可以通过 location 指令实现 Ngi ... 阅读更多
一、概述 server 模块是 http 的子模块,它用来定一个虚拟主机,一个 server 模块主要包含一下配置:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
server { # 监听的端口 listen 8080; # 用户访问的域名,请求的 Host 字段 server_name localhost 192.168.87.131; # 主目录地址 root /home/wwwroot; # 默认的首页形式 index index.php index.html index.htm; # 字符编码 charset utf-8; # 用户的访问日志,main 表示日志的格式 # 目录需要提前创建好 access_log logs/host.access.log main; # 错误日志,error 为日志格式 # 目录需要提前创建好 error_log logs/host.error.log error; .... } |
这个配置的意思是当有用户在浏览器中输入 192.168.87.131:8080 访问主机的 8080 端口时,服务器将会返回/hom ... 阅读更多
一、概述 二、 nginx 配置 nginx 的配置文件位于/usr/local/nginx/conf/nginx.conf,根据安装位置的不同而不同,也有可能在/etc/nginx/nginx.con,打开文件:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 |
[root@localhost conf]# cat /usr/local/nginx/conf/nginx.conf #user root; worker_processes 1; #error_log logs/error.log; #error_log logs/error.log notice; #error_log logs/error.log info; #pid logs/nginx.pid; events { worker_connections 1024; } http { include /etc/nginx/conf/*.conf; include mime.types; 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; sendfile on; #tcp_nopush on; #keepalive_timeout 0; keepalive_timeout 65; #gzip on; server { listen 80; server_name localhost; #charset koi8-r; #access_log logs/host.access.log main; location / { root html; index index.html index.htm; } #error_page 404 /404.html; # redirect server error pages to the static page /50x.html # error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } # proxy the PHP scripts to Apache listening on 127.0.0.1:80 # #location ~ .php$ { # proxy_pass https://127.0.0.1; #} # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000 # #location ~ .php$ { # root html; # fastcgi_pass 127.0.0.1:9000; # fastcgi_index index.php; # fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name; # include fastcgi_params; #} # deny access to .htaccess files, if Apache"s document root # concurs with nginx"s one # #location ~ /.ht { # deny all; #} } # another virtual host using mix of IP-, name-, and port-based configuration # #server { # listen 8000; # listen somename:8080; # server_name somename alias another.alias; # location / { # root html; # index index.html index.htm; # } #} # HTTPS server # #server { # listen 443 ssl; # server_name localhost; # ssl_certificate cert.pem; # ssl_certificate_key cert.key; # ssl_session_cache shared:SSL:1m; # ssl_session_timeout 5m; # ssl_ciphers HIGH:!aNULL:!MD5; # ssl_prefer_server_ciphers on; # location / { # root html; # index index.html index.htm; # } #} } |
一共分为六 ... 阅读更多
创建文件/etc/init.d/nginx,把脚本复制进去,nginx 官方启动脚本:nginx 官方脚本地址
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 |
#!/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/sbin/nginx" prog=$(basename $nginx) NGINX_CONF_FILE="/etc/nginx/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:.*--user=" | sed "s/[^*]*--user=([^ ]*).*/1/g" -` if [ -n "$user" ]; then 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 fi } 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 |
需要修改的两个地方:
|
1 2 3 |
# nginx 程序目录 # nginx="/usr/sbin/nginx" nginx="/usr/local/nginx/sbin/nginx" |
[crayon-694d ... 阅读更多
一、摘要 nginx 安装有三种方式: 1 、配置 yum 安装:这个需要先配置好 yum 软件包,然后使用 yum install nginx 命令安装。 2 、源码安装:去官网下载源码,自行编译安装。 3 、使用第三方提供的安装包:如 lnmp 一键安装包,简单操作就能一键配置。 这里我使用的是第二种方式,从源码安装。 ... 阅读更多
一、摘要 早就听说了强大的 nginx,一直想找个机会好好了解一下,可惜网上的资料纷杂不一,大部分都是直接把配置文件一扔,很少去解释每个配置的意义,看得天花乱坠,知其然不知其所以然。所以下定决心自己好好动手去玩一玩,顺便记录下这一个神奇的过程~ 二、 nginx 介绍 Nginx (engine x) 是 ... 阅读更多