
Nginx 是一套相當強悍又好用的伺服器,特別在 reverse proxy 或是 load balancce 的使用上,都非常的方便好用。
但一般都是用在 web 方便的應用較多。如果是其他服務,其實可以透過
nginx_tcp_proxy_module
達到代理或是 Load balance 的目的喔!!~預備安裝環境
yum -y install proc* openssl* pcre*
其他相關套件一起裝一下
yum -y install telnet ntpdate sntp unzip wget mlocate net-tools mailx iftop id3lib libid3tag
下載 nginx source code
其餘的版本需要測試過,這個版本是已經測試過的
wget http://nginx.org/download/nginx-1.9.4.tar.gz tar zxvf nginx-1.9.4.tar.gz
準備 Build
cd nginx-1.9.4 ./configure --prefix=/etc/nginx --sbin-path=/usr/sbin/nginx --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --pid-path=/var/run/nginx.pid --lock-path=/var/run/nginx.lock --http-client-body-temp-path=/var/cache/nginx/client_temp --http-proxy-temp-path=/var/cache/nginx/proxy_temp --http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp --http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp --http-scgi-temp-path=/var/cache/nginx/scgi_temp --user=nginx --group=nginx --with-http_ssl_module --with-http_realip_module --with-http_addition_module --with-http_sub_module --with-http_dav_module --with-http_flv_module --with-http_mp4_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_random_index_module --with-http_secure_link_module --with-http_stub_status_module --with-http_auth_request_module --with-threads --with-stream --with-stream_ssl_module --with-mail --with-mail_ssl_module --with-file-aio --with-ipv6 --with-http_spdy_module --with-cc-opt='-O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector --param=ssp-buffer-size=4 -m64 -mtune=generic' make; make install
相關設定
以下範例說明透過 stream 的方式連結 HTTPS/443。這個做法有別於一般 Web Proxy,因為單純的就是把 TCP port forward 過去而已。所以不需要另外再設定 SSL 等。但同時也不具有 Cache 的效果就是了。
user nobody; worker_processes 1; events { worker_connections 1024; } stream { server { listen 443; proxy_pass admin; } upstream admin { server admin.uim.cloud:443; } }
如果連結的 port 是 udp,只要這樣設定即可
server { listen 53 udp; proxy_pass DNS; } upstream DNS{ server 8.8.8.8:53; }
設為服務啟動
在 /usr/lib/systemd/system/ 下新增 nginx.service
vi /usr/lib/systemd/system/nginx.service
內容如下
[Unit] Description=nginx After=network.target remote-fs.target nss-lookup.target [Service] Type=forking PIDFile=/var/run/nginx.pid ExecStartPre=/usr/sbin/nginx -t -c /etc/nginx/nginx.conf ExecStart=/usr/sbin/nginx -c /etc/nginx/nginx.conf ExecReload=/usr/sbin/nginx -s reload ExecStop=/usr/sbin/nginx -s stop ExecQuit=/usr/sbin/nginx -s quit PrivateTmp=true [Install] WantedBy=multi-user.target
啟用開機自動啟動
systemctl enable nginx
立刻啟動
systemctl start nginx
開啟防火牆
設定好後,當然要開啟防火牆的 Port,不然還是不會動喔!~
firewall-cmd --permanent --add-port=443/tcp firewall-cmd --reload