一、目录
1 linux 下安装nginx
2. nginx 配置
3. nginx 常用命令
4. 安装报错
二、实现
1 linux 下安装nginx
1. 安装 openssl 、zlib 、 gcc 、pcre依赖
yum -y install make zlib zlib-devel gcc-c++ libtool openssl openssl-devel pcre pcre-devel
- 安装 nginx
a 官网下载nginx:http://nginx.org
//创建一个文件夹
cd /usr/soft
//创建一个目录作为nginx的安装目录
mkdir nginx
//下载tar包
wget http://nginx.org/download/nginx-1.13.7.tar.gz
tar -xvf nginx-1.13.7.tar.gz
b 使用命令解压,进入安装目录 >>cd nginx-1.13.7
指定路径>> ./configure
//指定nginx安装的目录
./configure --prefix=/usr/soft/nginx
c 执行指令: make && make install
安装成功之后会自动生成/usr/local/nginx文件夹(文件中包括nginx 命令、配置、日志、资源)
- 启动测试
./usr/local/nginx/sbin/nginx
访问服务器ip:80
# nginx默认监听端口是80,记得设置防火墙开放端口,或者直接关闭防火墙。
# 查看开放的端口
firewall-cmd --list-all
# 设置开放的端口
firewall-cmd --add-service=http --permanent
firewall-cmd --add-port=80/tcp --permanent
# 重启防火墙
firewall-cmd --reload
2. nginx 配置
1 nginx 做转发配置
实现效果:
使用nginx做反向代理,根据访问的路径跳转不通的服务。
nginx的监听端口为9001
访问http://127.0.0.1:9001/edu/ 跳转127.0.0.1:8080
访问http://127.0.0.1:9001/vod/ 跳转127.0.0.1:8081
找到nginx.conf进行配置
server{listen 9001;server_name localhost;location ~/edu/{proxy_pass http://127.0.0.1:8080/; }location ~/vod/ {proxy_pass http://127.0.0.1:8081/; }
}
注:localhost后面的相当于一个正则校验,之后匹配的才会跳转到对应的代理。 需要在8080、8081 后加/
nginx.conf 具体实现:
#user nobody;
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 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 8011;# 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 http://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 8091;#listen somename:8011;server_name localhost;location /edu {proxy_pass http://10.120.130.49:8021/;}}# 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;# }#}}
重启nginx.
配置解释:
配置解释
location [ = | ~ | ~* | ^~ ] uri {
}= :用于不含正则表达式的uri前,需要请求字符串与uri严格匹配,如果匹配成功,停止继续向下搜索并立即处理该请求。
~ :用于表示uri包含正则表达式,并且区分大小写
~* :用于表示uri包含正则表达式,并且不区分大小写
^~ :用于不含正则表达式的 uri 前,要求 Nginx 服务器找到标识 uri 和请求字符串匹配度最高的 location 后,立即使用此 location 处理请求,而不再使用 location 块中的正则 uri 和请求字符串做匹配。
注意:如果 uri 包含正则表达式,则必须要有 ~ 或者 ~* 标识。
- 负载均衡 配置
实现效果:浏览器地址栏输入:http://ip:9001/edu/a.html,负载均衡效果,平均到8080和8081端口。
找到nginx.conf进行配置
重启nginx 服务。
nginx 配置
[root@dockermain nginx]# cat conf/nginx.conf#user nobody;
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 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 8011;# 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 http://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#upstream myserver {server 10.120.130.49:8021; #意图服务1server 10.120.130.49:8020;#意图服务2}server {listen 8091;#listen somename:8011;server_name localhost;location /edu {proxy_pass http://myserver/;root html;}}# 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;# }#}}
3. nginx 常用命令
使用nginx操作命令的前提条件,必须进入nginx的目录cd /usr/local/nginx/sbin/
查看nginx版本号./nginx -v
启动nginx./nginx
关闭nginx./nginx -s stop
重新加载nginx./nginx -s reload修改端口:vim /conf/nginx.conf 中的80 端口 然后重启
4. 安装报错
src/http/ngx_http_script.c:698:18: error: cast between incompatible function types from ‘size_t (*)(ngx_http_script_engine_t *)’ {aka ‘long unsigned int (*)(struct <anonymous> *)’} to ‘void (*)(ngx_http_script_engine_t *)’ {aka ‘void (*)(struct <anonymous> *)’} [-Werror=cast-function-type]698 | code->code = (ngx_http_script_code_pt) ngx_http_script_copy_len_code;| ^
src/http/ngx_http_script.c: In function ‘ngx_http_script_add_var_code’:
src/http/ngx_http_script.c:787:18: error: cast between incompatible function types from ‘size_t (*)(ngx_http_script_engine_t *)’ {aka ‘long unsigned int (*)(struct <anonymous> *)’} to ‘void (*)(ngx_http_script_engine_t *)’ {aka ‘void (*)(struct <anonymous> *)’} [-Werror=cast-function-type]787 | code->code = (ngx_http_script_code_pt) ngx_http_script_copy_var_len_code;
解决:
解决:进入Makefile
vim /usr/local/nginx-1.14.0/objs/Makefile
在文件中找到:CFLAGS =-pipe -0 -w -Wall -Wpointer-arith -Wno-unused-parameter -Werror -g
把 -Werror 删掉,然后保存退出后,重新make
src/os/unix/ngx_user.c:26:7: error: ‘struct crypt_data’ has no member named ‘current_salt’26 | cd.current_salt[0] = ~salt[0];| ^解决:vim src/os/unix/ngx_user.c注释掉cd.current_salt[0] = ~salt[0]/*cd.current_salt[0] = ~salt[0] */