1、安装Nginx所需要的库、安装pcre库是为了使Nginx支持具备URL重写功能的rewrite模块、如果不安装pcer库,则Nginx无法使用rewrite模块功能,Nginx的rewrite模块功能几乎是企业应用必须的。
yum install pcre pcre-devel openssl openssl-devel -y
2、创建用户
useradd nginx -s /sbin/nologin -M
3、 将下载 好的nginx-1.12.0.tar.gz 解压
tar -zxvf nginx-1.12.0.tar.gz
4、进入解压包
cd nginx-1.12.0
5、创建文件
mkdir /usr/local/nginx1.12
6编译
./configure --user=nginx --group=nginx --prefix=/usr/local/nginx1.12/ --with-http_stub_status_module --with-http_ssl_module
如有其它模块添加
--add-module=/tmp/XXXX
make
make install
7创建软链接方便人员使用、方便以后升级
ln -s /usr/local/nginx1.12 /usr/local/nginx
8查看nginx
ls -l /usr/local/nginx/
9启动前检查配置文件语法
/usr/local/nginx/sbin/nginx -t
10、启动nginx
/usr/local/nginx/sbin/nginx
12、查看Nginx服务对应的端口是否成功启动
lsof -i :80
nginx配置
server {
listen 80;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root html/Club/public;
index index.html index.htm index.php ;
# if (!-e $request_filename) {
#rewrite ^(.*)$ /index.php?s=$1 last;
#break;
#}
# autoindex off;
}
location ~ .*\.(php|php5)?$
{
root html/Club/public;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
include fastcgi.conf;
}
}
location ~ \.php(.*)$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_split_path_info ^((?U).+\.php)(/?.+)$;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info;
fastcgi_param PATH_TRANSLATED $document_root$fastcgi_path_info;
include fastcgi_params;
}
隐藏nginx版本号
server_tokens off;
添加
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
location / {
index index.html index.htm index.php l.php;
autoindex on;
if (!-e $request_filename) {
rewrite ^/index.php(.*)$ /index.php?s=$1 last;
rewrite ^(.*)$ /index.php?s=$1 last;
break;
}
}
location = /callback {
root /alidata/txall/callback;
index index.html index.php index.htm;
rewrite "^(.*)/callback$" /callback/web/ last;
}
location ^~ /End/public {
rewrite ^/End/public/(.*)$ /$1 break;
proxy_pass http://www.isetan2.com;
}
location ~ \.php(.*)$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_split_path_info ^((?U).+\.php)(/?.+)$;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info;
fastcgi_param PATH_TRANSLATED $document_root$fastcgi_path_info;
include fastcgi_params;
}
正反向代理配置
patch -p1 < /root/ngx_http_proxy_connect_module-master/proxy_connect.patch
./configure --add-module=/root/ngx_http_proxy_connect_module-master/ngx_http_proxy_connect_module
make && make install
编辑安装后配置
#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;
#access_log logs/access.log main;
sendfile on;
#tcp_nopush on;
keepalive_timeout 65;
#gzip on;
server {
listen 88; #监听端口
resolver 183.60.82.98; #dns解析地址
server_name _;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
proxy_pass https://$host$request_uri; #设定http代理服务器的协议和地址
proxy_set_header HOST $host;
proxy_buffers 256 4k;
proxy_max_temp_file_size 0k;
proxy_connect_timeout 30;
proxy_send_timeout 60;
proxy_read_timeout 60;
proxy_next_upstream error timeout invalid_header http_502;
#root html;
#index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
server {
resolver 8.8.8.8; #dns解析地址
listen 89; #代理监听端口
proxy_connect;
proxy_connect_allow 443 563;
location / {
proxy_pass https://$host$request_uri; #设定https代理服务器的协议和地址
proxy_set_header HOST $host;
proxy_buffers 256 4k;
proxy_max_temp_file_size 0k;
proxy_connect_timeout 30;
proxy_send_timeout 60;
proxy_read_timeout 60;
proxy_next_upstream error timeout invalid_header http_502;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
}
当配置文件配置好之后保存即可,重启nginx,进行测试:
去内网服务器里curl,可以在环境变量里添加代理:
vim /etc/profile
...
...
#export https_proxy=正向代理IP:端口
export https_proxy=192.168.3.17:89
另一种 方式不用加环境变量,临时代理
#curl -i --proxy 代理IP:端口 要访问域名
curl -i --proxy 192.168.3.17:89 www.baidu.com