nginx安装并配置实现端口转发
1、將nginx程序包上傳至服務(wù)器,我這里使用的是nginx-1.21.6.tar.gz
2、解壓nginx?
3、解壓后會(huì)在當(dāng)前目錄下生成一個(gè)nginx-1.21.6目錄
4、進(jìn)入該目錄,執(zhí)行? ?--with-stream
5、運(yùn)行命令后可能會(huì)出現(xiàn)如下錯(cuò)誤,這表示你的服務(wù)器上沒(méi)有pcre 和 pcre-devel和zlib-devel這3? ? ? ?個(gè)包,通過(guò)yum命令進(jìn)行安裝
??? ?yum -y install pcre??? ?yum -y install pcre-devel??? ?yum -y install zlib-devel6、再次執(zhí)行第四步中的命令
?7、執(zhí)行命令進(jìn)行安裝
make && make install8、到這里nginx已經(jīng)安裝好了,接下來(lái)進(jìn)行nginx.conf文件的配置
? ? ? ? 配置文件路徑:/你的安裝路徑/nginx/conf/nginx.conf
?9、編輯配置文件,如下為nginx.conf 默認(rèn)內(nèi)容,在修改前請(qǐng)備份該文件
cp nginx.conf nginx_bak.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 {wor{er_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; "nginx.conf" 117L, 2656C 1,0-1 é????#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 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 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;# }#}}10、在配置文件中增加以下內(nèi)容
stream { --stream部分與http部分平級(jí)#這兩段實(shí)現(xiàn)的是10.170.12.159:19013端口通過(guò)本地服務(wù)器的22222端口轉(zhuǎn)發(fā)出去#其中下面的兩處mysqltest可以自己起名,但要保證名稱相同upstream mysqltest {server 10.110.111.159:19013;}server {listen 22222;proxy_pass mysqltest;} }11、增加內(nèi)容后的nginx.conf明細(xì)內(nèi)容如下,其中紅框部分為本次新增內(nèi)容,我在這里添加了兩個(gè)如需轉(zhuǎn)發(fā)更多端口在stream中繼續(xù)增加即可,用于進(jìn)行端口轉(zhuǎn)發(fā)(本配置文件內(nèi)容僅支持端口轉(zhuǎn)發(fā)功能)
12、啟動(dòng)nginx
cd /home/collect/nginx/sbin ./nginx -c /home/collect/nginx/conf/nginx.conf?
至此端口轉(zhuǎn)發(fā)已完成,可以使用了?
總結(jié)
以上是生活随笔為你收集整理的nginx安装并配置实现端口转发的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 华科计算机硕士毕业论文,华中科技大学硕士
- 下一篇: 20位诺奖得主齐聚一堂,世界科技创新论坛