linux下nginx安装和配置
一、软件安装
1、环境说明
操作系统:CentOS 7.4 64位
nginx版本:1.16.1
安装日期:2019/10/01
安装用户:root
2、安装运行库
yum -y install gcc gcc-c++ automake autoconf libtool make
1 | yum install gcc cmake make cmake unzip ncurses-devel gcc gcc -c++ -y |
yum install -y gcc-c++ zlib zlib-devel openssl openssl-devel pcre pcre-devel
3、安装nginx
$ cd /usr/local/src $ wget http://nginx.org/download/nginx-1.16.1.tar.gz $ tar -zxvf nginx-1.16.1.tar.gz $ cd nginx-1.16.1 $ ./configure --with-http_ssl_module --prefix=/usr/local/nginx $ make $ make install
二、软件使用命令
nginx默认安装路径为 /usr/local/nginx
启动:./sbin/nginx
关闭:/usr/local/nginx/sbin/nginx -s stop
重启:/usr/local/nginx/sbin/nginx -s reopen
重新加载配置文件:/usr/local/nginx/sbin/nginx -s reload
查看nginx的进程:
ps -ef | grep nginx
三、软件配置方法
#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; proxy_cache_path D:/nginx-1.16.1/cache levels=1:1:1 keys_zone=pcache:10m max_size=2g; #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 55556; server_name 127.0.0.1; proxy_cache pcache; #调用缓存 proxy_cache_key $request_uri; #把什么当键 proxy_cache_methods GET HEAD; #缓存哪些项 proxy_cache_valid 200 302 10m; #指定200 302响应码的内容缓存10分钟 proxy_cache_valid 404 1m; #指定404响应码的内容缓存1分钟 proxy_cache_use_stale http_502; #允许502响应码的内容使用过期缓存 proxy_set_header X-Real-IP $remote_addr; #将实际的客户端IP发送至后端服务器 add_header X-Via $server_addr; #将代理服务器的IP发至后端服务器 #charset koi8-r; #access_log logs/host.access.log main; location / { proxy_pass http://127.0.0.1:55555/; root html; index index.html index.htm; } location /nationInterface/ { proxy_pass http://124.163.214.106:18064/nationInterface/; root html; index index.html index.htm; proxy_set_header X-Real-IP $remote_addr; #将实际的客户端IP发送至后端服务器 add_header X-Via $server_addr; #将代理服务器的IP发至后端服务器 } location /CMAQ { autoindex on; root D:/dataCenter; add_header Cache-Control no-cache; add_header Pragma no-cache; add_header Expires 0; } #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; # } #} }
注意事项:配置静态目录的时候,采用的是目录拼接的方式,比如上图的静态目录是:D:/dataCenter/CMAQ/。
如果linux下遇到权限问题,比如 13: Permission denied。从以下几个方面排查:
1、root安装的,那么启动也要用root用户
2、nginx.conf配置文件第一行,#user nobody要改成user root
3、是否缺少index.html
4、修改目录权限:chmod -R 777 /data
5、要将SELinux设置为关闭。
开机自动启动-制作nginx服务(再/etc/rc.local中添加执行脚本的方法不行):
第一步:进入到/lib/systemd/system/目录
[root@iz2z init.d]# cd /lib/systemd/system
第二步:创建nginx.service文件,并编辑
# vim nginx.service
内容如下:
[Unit] Description=nginx service After=network.target [Service] Type=forking ExecStart=/usr/local/nginx/sbin/nginx ExecReload=/usr/local/nginx/sbin/nginx -s reload ExecStop=/usr/local/nginx/sbin/nginx -s quit PrivateTmp=true [Install] WantedBy=multi-user.target
第三步:加入开机自启动
# systemctl enable nginx
其他常用命令:
# systemctl start nginx.service 启动nginx服务 # systemctl stop nginx.service 停止服务 # systemctl restart nginx.service 重新启动服务 # systemctl list-units --type=service 查看所有已启动的服务 # systemctl status nginx.service 查看服务当前状态 # systemctl enable nginx.service 设置开机自启动 # systemctl disable nginx.service 停止开机自启动
nginx,配置文件目录浏览
location /gfs {
autoindex on;
root //public/share;
autoindex_exact_size off;
autoindex_localtime on;
charset utf-8;
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 上周热点回顾(3.3-3.9)
2015-12-30 flex datagrid 换行